alp*_*pha 0 c# generics runtime activator
在以下示例中,我可以通过字符串动态创建对象; 但是,我无法获得BASE类的公共方法.我无法将obj转换为BASE因为我不知道在设计时将使用什么泛型.在运行时这样做的任何建议都会很好.
项目A包含A类{T,J>:BASE {T,J>
项目B包含B类{T,J>:BASE {T,J>
Project C包含Class BASE {T,J>
public virtual control {T,J> item
Project Windows Form
cmdGo_Click事件
string dll = textbox1.text // ex"ProjectA.dll"
string class = textbox2.text // ex"A`2 [enuT,enuJ]"
object obj = activator.createinstancefrom(dll,class)
此代码创建BASE <int,string>的实例:
Type type = typeof(BASE<,>).MakeGenericType(typeof(int), typeof(string));
object instance = Activator.CreateInstance(type);
Run Code Online (Sandbox Code Playgroud)