我有一个类(TabControlH60),它继承自基类(UserControl)并实现一个接口(IFrameworkClient).我使用.NET Activator类实例化对象.使用返回的实例,我可以转换为UserControl基类,但不能转换为接口.我得到的例外是在代码snipet下面.如何转换为界面?
object obj = Activator.CreateInstance(objType);
Type[] interfaces = obj.GetType().GetInterfaces(); // contains IFrameworkClient
m_Client = (UserControl)obj; // base class cast works
IFrameworkClient fc = (IFrameworkClient)obj; // interface cast fails
// Note: The (IFrameworkClient)obj cast works fine in the debugger Watch window.
{"Unable to cast object of type 'FPG.H60.AFF.TabControlH60' to type
'FPG.AFF.Interfaces.IFrameworkClient'."}
Run Code Online (Sandbox Code Playgroud)