2

我正在ASP.NET Web 应用程序中做一个项目。 我不能将对象转换为接口。这是我的代码:

str2 = myXmlTextReader.ReadInnerXml();  
Type myTypeObj = testAssembly.GetType(str2);  
Object obj = Activator.CreateInstance(myTypeObj);  
Imapper EM = (Imapper)obj; 

在 Windows 窗体应用程序中,我没有任何问题。下面的代码也返回空值:

Imapper EM = obj as Imapper;  
4

1 回答 1

2

Your Exam_Mapper type needs to implement IMapper in order for the code to work. It apparently does not. Either that or you have two types, IMapper and Imapper (you use both names in your question; possible typo), and you're confusing the two.

Update try this simple example, and see if it works:

interface ITest { }
class Test : ITest { }
//in a method
Object obj = new Test();
ITest test = (ITest)obj;

Now compare to your situation. What's the difference?

于 2012-09-05T13:48:54.590 回答