[英]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.