這是我的情況。我在C#中使用WMI。謝天謝地,我發現MgmtClassGen.exe它生成了我現在需要的所有類。希望在C#中使用靜態抽象函數。任何解決方法?
但是,我一直在瀏覽自動生成的類,並將通用代碼拆分爲Utility類或基類。到目前爲止這麼好,清理了很多代碼。但我碰到了一個障礙。每個類都有幾個(大約8個)被稱爲GetInstances
的靜態函數。基本上有2個重載,另一個功能只是提供默認參數。
我想將這些函數放在基類中,因爲它們在所有類中都是相同的,除了3個變量。即,對象的ClassName
(如「MicrosoftDNS_Zone
」),對象的Namespace
(如「root\microsoftdns
」)和ManagementScope
對象。
我目前所做的是這樣的;將這兩個函數中的代碼移到基類中,並添加了3個參數,用於上面列出的3個差異。但是,這仍然需要在每個班8個功能,只是調用基類與ClassName
,Namespace
,並填寫Scope
參數。
- 有什麼辦法來重構這個 代碼,這樣我不需要方法 每個派生類中的「包裝器」?
- 我可以只聲明基類中的靜態 方法和 以某種方式得到來自 的派生類的ClassName等嗎?
- 反射甚至會在這裏工作嗎?
基類:
namespace WMI
{
public abstract class Object : System.ComponentModel.Component
{
// ...
protected static WMI.ManagementTypeCollection GetInstances(string className, string namespaceName, ManagementScope statMgmtScope, ManagementScope mgmtScope, EnumerationOptions enumOptions, Func<ManagementObject,WMI.Object> del)
{
if((mgmtScope == null))
{
if((statMgmtScope == null))
{
mgmtScope = new System.Management.ManagementScope();
mgmtScope.Path.NamespacePath = namespaceName;
}
else
{
mgmtScope = statMgmtScope;
}
}
System.Management.ManagementPath pathObj = new System.Management.ManagementPath();
pathObj.ClassName = className;
pathObj.NamespacePath = namespaceName;
System.Management.ManagementClass clsObject = new System.Management.ManagementClass(mgmtScope, pathObj, null);
if((enumOptions == null))
{
enumOptions = new System.Management.EnumerationOptions();
enumOptions.EnsureLocatable = true;
}
return new WMI.ManagementTypeCollection(clsObject.GetInstances(enumOptions), del);
}
protected static WMI.ManagementTypeCollection GetInstances(string className, string namespaceName, ManagementScope statMgmtScope, ManagementScope mgmtScope, string condition, String[] selectedProperties, Func<ManagementObject, WMI.Object> del)
{
if((mgmtScope == null))
{
if((statMgmtScope == null))
{
mgmtScope = new System.Management.ManagementScope();
mgmtScope.Path.NamespacePath = namespaceName;
}
else
{
mgmtScope = statMgmtScope;
}
}
System.Management.ManagementObjectSearcher ObjectSearcher = new System.Management.ManagementObjectSearcher(mgmtScope, new SelectQuery(className, condition, selectedProperties));
System.Management.EnumerationOptions enumOptions = new System.Management.EnumerationOptions();
enumOptions.EnsureLocatable = true;
ObjectSearcher.Options = enumOptions;
return new WMI.ManagementTypeCollection(ObjectSearcher.Get(), del);
}
}
}
派生類:
namespace WMI.MicrosoftDNS
{
public class AAAAType : WMI.Object
{
private static string CreatedWmiNamespace = "root\\microsoftdns";
private static string CreatedClassName = "MicrosoftDNS_AAAAType";
private static System.Management.ManagementScope statMgmtScope = null;
// ...
public static WMI.ManagementTypeCollection GetInstances()
{
return GetInstances(null, null, null);
}
public static WMI.ManagementTypeCollection GetInstances(string condition)
{
return GetInstances(null, condition, null);
}
public static WMI.ManagementTypeCollection GetInstances(System.String[] selectedProperties)
{
return GetInstances(null, null, selectedProperties);
}
public static WMI.ManagementTypeCollection GetInstances(string condition, System.String[] selectedProperties)
{
return GetInstances(null, condition, selectedProperties);
}
public static WMI.ManagementTypeCollection GetInstances(ManagementScope mgmtScope, EnumerationOptions enumOptions)
{
return WMI.Object.GetInstances(CreatedClassName, CreatedWmiNamespace, statMgmtScope, mgmtScope, enumOptions, mo => new AAAAType(mo));
}
public static WMI.ManagementTypeCollection GetInstances(ManagementScope mgmtScope, string condition)
{
return GetInstances(mgmtScope, condition, null);
}
public static WMI.ManagementTypeCollection GetInstances(ManagementScope mgmtScope, System.String[] selectedProperties)
{
return GetInstances(mgmtScope, null, selectedProperties);
}
public static WMI.ManagementTypeCollection GetInstances(ManagementScope mgmtScope, string condition, System.String[] selectedProperties)
{
return WMI.Object.GetInstances(CreatedClassName, CreatedWmiNamespace, statMgmtScope, mgmtScope, condition, selectedProperties, mo => new AAAAType(mo));
}
}
}
對於任何人想知道中,func <的ManagementObject,WMI.Object>是一個黑客用於返回集合項作爲WMI.Object而不是System.Management.ManagementObject用於我的代碼的其餘部分。 – TJMonk15
爲什麼不做collection.Cast().ToList(); –
user76035
由於System.Management.ManagementObjects不是WMI.Objects :-P – TJMonk15