我可以声明一个 Type类型的变量吗?编译时不指定T?
如何动态加载“MyContent”类? 我有 1 个 interface
、1 个抽象通用 class
和 1 个类。检查我的代码:
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 }
}
我正在尝试,但显然它不起作用:
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
更改为 IMyContentInterface
,则有效:
IMyContentInterface<MyObject> content = (IMyContentInterface<MyObject>)Assembly.Load("MyAssembly").CreateInstance("MyContent");
content.MyMethod();
//assembly and type names are correct
问题是我不'定义 IMyContentInterface
时,第二行中的对象是什么。请问,有人知道如何在 .NET Framework 4.0 中做到这一点吗?
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
If I change IMyContentInterface<obj>
to IMyContentInterface<MyObject>
, works :
IMyContentInterface<MyObject> content = (IMyContentInterface<MyObject>)Assembly.Load("MyAssembly").CreateInstance("MyContent");
content.MyMethod();
//assembly and type names are correct
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
< 中的项目>
必须是类型而不是对象。我的汽车是汽车类型的对象,所以
我想要一个列表来保存我的汽车(汽车类型的对象)。
然后我们将汽车类型的对象添加到我的列表中。
the item in the
< >
has to be a type not an object.my car is an object of the type car so
i want a list to keep my cars (objects of type Car) in.
And then we add object of type Car to my List.
加载并不难 - 您已经知道如何做到这一点,但 C# 泛型是强类型的,在编译时进行检查和保证。考虑以下代码:
如果允许您像这样声明泛型,C# 编译器无法告诉您这是非法的:
如果您的最终目标是调用
MyContent.MyMethod()
并且没有与泛型类型参数
有关的任何内容,请考虑声明一个可以在继承层次结构中的某个位置实现的非泛型接口,并使用该接口声明实例变量:Loading it isn't hard - you already know how to do that, but C# generics are strongly-typed, checked and guaranteed at compile time. Consider this code:
The C# compiler couldn't tell you this was illegal if you were allowed to declare generics like this:
If your ultimate goal is to call
MyContent.MyMethod()
and that doesn't have anything to do with the generic type parameter<T>
, consider declaring a non-generic interface you can implement somewhere in your inheritance hierarchy and declare your instance variable using that:我读了几遍这篇文章,但我明白了你在问什么。 :) 这个问题是另一个问题的具体实例:
也就是说,下面是一个示例,说明如何将它用于测试用例。显然你可以改变它。另外,不要错过我在本答案末尾的最后注释。
Assembly MyCompany.MyProduct.MyComponent:
在此程序集中定义接口:
Assembly MyCompany.MyProduct.MyComponent.Implementation:
在此程序集中实现将动态加载的接口。
程序集 MyCompany.MyProduct
您的程序在此程序集中组成,这是我从托管扩展性中提取的术语框架。假定接口更有可能保留 与产品开发期间的实现兼容。这种设计试图支持内聚而非耦合(一对经常被误解的词),但是实际的实施在能否成功实现这一目标方面往往存在很大差异。
最后说明:
托管可扩展性框架是作为您正在处理的问题的通用解决方案而构建的。在使用它一段时间后,我自信地说它具有以下良好的特性:
如果它满足以下一个或多个条件的任意组合,我会很容易地推荐它作为开发新应用程序的人的严肃可行的选择:
I had to read this a few times, but I figured out what you're asking. :) This question is a specific instance of this other question:
That said, here's an example of how you might use it for your test case. Obviously you can vary it. Also, don't miss my final note at the end of this answer.
Assembly MyCompany.MyProduct.MyComponent:
Define your interfaces in this assembly:
Assembly MyCompany.MyProduct.MyComponent.Implementation:
Implement the interfaces in this assembly that will be dynamically loaded.
Assembly MyCompany.MyProduct
Your program is composed in this assembly, a term I pulled from the Managed Extensibility Framework. This assembly references
MyCompany.MyProduct.MyComponent
but notMyCompany.MyProduct.MyComponent.Implementation
under the assumption that the interfaces are more likely to remain compatible than the implementations during product development. This design is an attempt to favor cohesion over coupling (a pair of often misunderstood words), but the actual implementations tend to vary heavily in their success of achieving this goal.Final Note:
The Managed Extensibility Framework was built as a common solution to the problem you are working on. Having worked with it for a while now, I say with confidence that it has the following nice properties:
I would easily recommend it as serious viable option for someone working on a new application if it meets any combination of one or more of the following:
这是一种动态加载接口的方法。这假设您有某种方式获取您尝试从中加载它的程序集以及类型名称的字符串。
就我而言,我使用了 Xml 文件。您可以使用任何方法,我不会展示这些方法,因为它可能会根据您的实现而改变。
This is a way to dynamically load a Interface. This assumes you have some way of getting the assembly you are trying to load it from and a string for the name of the type.
In my case I used an Xml file. You can use any, I don't show those methods, because it can change per your implementation.