[英]C# - Type conversion issue when calling base class method from derived class
我需要從Active Directory中檢索擴展屬性,為此在ComputerPrincipal
類中有一個私有方法。 我了解我只能通過派生類訪問私有方法,因此我從基類派生了一個類ComputerPrincipalEx
。 然后,我在派生類中創建(定義了?)一個方法,該方法在基類中調用了私有方法。 這部分似乎還可以。
當我嘗試使用基類的(公共)方法為具有派生類類型的變量分配值時,就會出現問題。 這是代碼,然后我將嘗試解釋更多內容:
派生類:
public class ComputerPrincipalEx : ComputerPrincipal
{
public ComputerPrincipalEx(PrincipalContext context) : base(context) { }
public ComputerPrincipalEx(PrincipalContext context, string samAccountName, string password, bool enabled) : base(context, samAccountName, password, enabled) { }
public string ExtensionGet(string extendedattribute)
{
return (string) base.ExtensionGet(extendedattribute)[0];
}
}
問題代碼本身就是我創建的另一個類的方法:
public string GetExtendedAttribute(string attributeName) { PrincipalContext ctx = new PrincipalContext(ContextType.Domain); ComputerPrincipalEx cp = new ComputerPrincipalEx(ctx); cp = ComputerPrincipalEx.FindByIdentity(ctx, Name); return cp.ExtensionGet(attributeName); }
ExtensionGet
是我需要公開的基類的私有方法,並且我認為我是對的,因為一旦創建了ComputerPrincipalEx
類型的對象,我就可以訪問ExtensionGet
,否則它將無法訪問。
問題是此行中的類型轉換:cp = ComputerPrincipalEx.FindByIdentity(ctx,Name);
cp
定義為ComputerPrincipalEx;
ComputerPrincipalEx.FindByIdentity
引用基本的ComputerPrincipal.FindByIdentity
方法,並返回ComputerPrincipal
。 編譯器反對類型之間的隱式轉換。 將ComputerPrincipal
強制轉換為ComputerPrincipalEx
可以滿足編譯器的要求,但該應用程序由於無法執行轉換而在運行時崩潰。
現在,我幾乎了解了所有這些內容,但是我假設必須有某種方法可以從基類中調用方法並將有效數據返回給派生類類型的對象,這就是我希望的找出方法。
在派生的ComputerPrincipalEx
類中,我需要隱藏基類的FindByIdentity
方法,並將其重新定義為調用FindByIdentityWithType
。 這讓我將返回類型強制轉換為ComputerPrincipalEx
,從而訪問ExtensionGet
方法。 (希望我能正確解釋;對我來說,這仍然是很新的。)
為了使它起作用,還需要添加以下幾行。 否則,編譯器將引發錯誤,指出ComputerPrincipalEx
不是要搜索的有效對象類型。
[DirectoryObjectClass("computer")]
[DirectoryRdnPrefix("CN")]
這是ComputerPrincipalEx
類的相關摘錄:(是的,我知道try / catch塊需要一些工作。)
[DirectoryObjectClass("computer")]
[DirectoryRdnPrefix("CN")]
public class ComputerPrincipalEx : ComputerPrincipal
{
public ComputerPrincipalEx(PrincipalContext context) : base(context) { }
public ComputerPrincipalEx(PrincipalContext context, string samAccountName, string password, bool enabled) : base(context, samAccountName, password, enabled) { }
new public string ExtensionGet(string extendedattribute)
{
try
{
if (base.ExtensionGet(extendedattribute).Length != 1)
{
return null;
}
else
{
return (string)base.ExtensionGet(extendedattribute)[0];
}
}
catch (Exception ex)
{
// This should be broken down to individual exceptions
string message = string.Format("Exception occurred while retrieving extended attribute {0}. \r\nThe following error occurred:\r\n {1}", extendedattribute, ex);
MessageBox.Show(message);
Application.Exit();
return null;
}
}
public static new ComputerPrincipalEx FindByIdentity(PrincipalContext ctx, string identityValue)
{
return (ComputerPrincipalEx)FindByIdentityWithType(ctx, typeof(ComputerPrincipalEx), identityValue);
}
}
和修改后的GetExtendedAttribute
調用:
public string GetExtendedAttribute(string attributeName)
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
var cp = ComputerPrincipalEx.FindByIdentity(ctx, Name);
return cp.ExtensionGet(attributeName);
}
謝謝大家的幫助。 希望這可以幫助遇到相同問題的其他人。
更改為此:
public string GetExtendedAttribute(string attributeName)
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
var cp = ComputerPrincipalEx.FindByIdentity(ctx, Name);
return cp.ExtensionGet(attributeName);
}
在我的印象中,您需要ComputerPrincipalEx中的一個構造函數(或靜態方法),該構造函數是從基類的實例創建的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.