问题描述
我如何加载类为myContent动态?我有1 接口< T>
,1抽象的一般类< T>
和1类。检查我的code OUT:
How do I Load the class "MyContent" dynamically ?I have 1 interface<T>
, 1 abstract generic class<T>
and 1 class. Check my code out:
public interface IMyObjectInterface{
}
public abstract MyAbstractObject : IMyObjectInterface{
}
public class MyObject : MyAbstractObject{
}
public interface IMyContentInterface<T> where T : MyAbstractObject
{
void MyMethod();
}
public abstract MyAbstractContent<T>, IMyContentInterface<T> where T : MyAbstractObject
{
public abstract void MyMethod();
}
public public class MyContent : MyAbstractContent<MyObject>
{
public override void MyMethod() { //do something }
}
我想,但显然它不工作:
I am trying but obviously it's not working:
IMyObjectInterface obj = (IMyObjectInterface)Assembly.Load("MyAssembly").CreateInstance("MyObject");
IMyContentInterface<obj> content = (IMyContentInterface<obj>)Assembly.Load("MyAssembly").CreateInstance("MyContent");
content.MyMethod();
//assembly and type names are correct
如果我修改 IMyContentInterface&LT; OBJ&GT;
到 IMyContentInterface&LT;为MyObject&GT;
,作品:
IMyContentInterface<MyObject> content = (IMyContentInterface<MyObject>)Assembly.Load("MyAssembly").CreateInstance("MyContent");
content.MyMethod();
//assembly and type names are correct
现在的问题是,我不正在发生的事情是我在2号线的对象,定义何时 IMyContentInterface&LT; T&GT;
。请,没有人知道该怎么做,在.NET框架4.0?
The problem is that i don't what is going to be my object in the 2nd line, when defining IMyContentInterface<T>
. Please, does somebody know how to do it in .NET Framework 4.0?
推荐答案
在&LT的项目; &GT;
必须是一个类型不是对象
the item in the < >
has to be a type not an object.
我的车是该类型车的对象,因此
my car is an object of the type car so
Car myCar=new Car();
我希望有一个列表,以保持我的车(轿车型的对象)的
i want a list to keep my cars (objects of type Car) in.
List<Car> myCars = new List<Car>();
然后我们添加型轿车的对象到我的清单。
And then we add object of type Car to my List.
myCars.Add(myCar);
myCars.Add(anotherCar);
这篇关于我可以宣布类型℃的变量; T&GT;不指定T对于编译时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!