我可以宣布类型℃的变量; T>不指定T对于编译时? [英] Can I declare a variable of Type<T> without specifying T at compile time?

查看:157
本文介绍了我可以宣布类型℃的变量; T>不指定T对于编译时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何加载类为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对于编译时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆