【发布时间】:2012-05-31 19:54:03
【问题描述】:
假设我有一个名为 myPage 的网页,它实现了 Page,但也实现了我自己的名为 myInterface 的接口。我的目标是在 myInterface 中调用一个名为 myFunction 的函数,只使用字符串中的类名。
public interface MyInterfac{
Myfunction();
}
public partial class MyPage1: Page, MyInterface{
Myfunction(){ return "AAA"; }
}
public partial class MyPage2: Page, MyInterface{
Myfunction(){ return "BBB"; }
}
下面是我可以得到的信息:
string pageName1 = "MyPage1";
string pageName2 = "MyPage2";
如何从这里到达沿线的东西:
(MyInterface)MyPage1_instance.Myfunction(); //Should return AAA;
(MyInterface)MyPage2_instance.Myfunction(); //Should return BBB;
编辑:这是当我尝试创建 MyInterface 的一个实例但它不起作用时:
Type myTypeObj = Type.GetType("MyPage1");
MyInterface MyPage1_instance = (MyInterface) Activator.CreateInstance (myTypeObj);
【问题讨论】:
-
你可能在这个问题背后有更多的东西......否则会是stackoverflow.com/questions/648160/…的重复
-
好吧,我试过了,但没用,因为我认为它是部分课程。
-
好的,它是重复的,我添加了命名空间,现在它可以工作了。