我想知道我正在做的以下类型转换是否正确?
Interface IA
{
string AName {get;set;}
string A_Address {get;set;}
UpdateA_Address();
}
Interface IB
{
string BName {get;set;}
string B_Address {get;set;}
UpdateB_Address();
}
public class A : IA
{
///implemented both prop
public void MethodNotInterfaceA()
{
//Some stuff
}
}
public class B : IB
{
///implemented both prop
public void MethodNotInterfaceB()
{
//Some stuff
}
}
public class X
{
IA aObj {get;set;}
IB bObj {get;set;}
public x()
{
aObj = new A();
bObj = new B();
}
public void SomeMethod1()
{
//some stuff here
aObj.UpdateB_Address();
aobj.MethodNotInterfaceB(); =============> is telling method not impemeted not part of interface
(aObj as A).MethodNotInterfaceB(); =============> this casting an interface to an class type is valid
}
public void SomeMethod2()
{
//some stuff here
bObj.UpdateB_Address();
bObj.MethodNotInterfaceB(); =============> is telling method not impemeted not part of interface
(bObj as B).MethodNotInterfaceB(); =============> this casting an interface to an class type is valid
}
}
这些接口对于其他类是通用的,因此MethodNotInterfaceB()不是它的一部分,我想在类X中访问它,所以从接口类型转换为类对象。
请告诉我这是否有效并且错误处理我需要在进行类型转换时小心。
仅供参考 - 它正在建设中没有任何错误,并且工作enter code here
也很好
谢谢, 维诺德