简体   繁体   English

Activator.CreateInstance与继承的类不在同一个项目中

[英]Activator.CreateInstance with inherited class not in the same project

I have a solution that contains two projects. 我有一个包含两个项目的解决方案。 First of let me describe my scenario. 首先让我来描述一下我的情景。 Project 1: holds a base class calls MyBaseClass with the following two property and method that i'm interested in: 项目1:持有一个基类调用MyBaseClass,其中包含以下两个我感兴趣的属性和方法:

TypeToLoad - static string property of which inherited class to instantiate with simple get/set functionality. TypeToLoad - 要使用简单的get / set功能实例化的继承类的静态字符串属性。

Instance - static property which returns a MyBaseClass instance of the type specified in TypeToLoad with the following lines of code: Instance - 静态属性,返回TypeToLoad中指定类型的MyBaseClass实例,代码如下:

public static MyBaseClass Instance{
   return (MyBaseClass)Activator.CreateInstance(Type.typeof(TypeToLoad));
}

now in the second project I inherit MyBaseClass as follow(of course proper namespace and references have been added). 现在在第二个项目中,我继承了MyBaseClass,如下所示(当然还添加了适当的命名空间和引用)。

public class MyInheritClass : MyBaseClass {
    //implements all other functions of MyBaseClass;
}

and one of my other code file, in the same project as MyInheritClass, I have the following lines of code: 和我的其他代码文件之一,在与MyInheritClass相同的项目中,我有以下几行代码:

Type myType = Type.typeof("MyInheritClass"); //returns the correct "Type"
MyBaseClass.TypeToLoad = "MyInheritClass";
MyBaseClass myInstance = MyBaseClass.Instance; //Errors... because Type.typeof(...) could not resolve "MyInheritClass".

When I move MyInheritClass code file into the same project as MyBaseClass the code runs just fine. 当我将MyInheritClass代码文件移动到与MyBaseClass相同的项目时,代码运行得很好。

My question is, is there anyway to create and instance of MyInheritClass using MyBaseClass.Instance by setting MyBaseClass.TypeToLoad = "MyInheritClass" without having to have these two classes in the same project (MyBaseClass in the base DLL class library and MyInheritClass in another project referencing MyBaseclass DLL library) ? 我的问题是,无论如何通过设置MyBaseClass.TypeToLoad =“MyInheritClass”使用MyBaseClass.Instance来创建和实例MyInheritClass,而不必在同一个项目中有这两个类(基本DLL类库中的MyBaseClass和另一个项目中的MyInheritClass)引用MyBaseclass DLL库)?

I've tried to search for reflections, Activator but wasn't able to find what I was looking for. 我试图搜索反射,Activator但无法找到我想要的东西。 Maybe I don't know the correct terminology for this. 也许我不知道这个的正确术语。

Assuming you actually mean Type.GetType() (and not Type.typeof() , which won't compile), then it's clearly documented as doing exactly what you're observing: 假设你实际上是指Type.GetType() (而不是Type.typeof() ,它将无法编译),那么它清楚地记录为完全正在你所观察到的:

The assembly-qualified name of the type to get. 要获取的类型的程序集限定名称。 […] If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace. [...]如果类型在当前正在执行的程序集中或在Mscorlib.dll中,则提供由其名称空间限定的类型名称就足够了。

It also explains how to fix it: ad the name of the assembly to the type name. 它还解释了如何修复它:将程序集的名称添加到类型名称。 So, if your type is called in MyInheritClass , it's in the MyNamespace namespace and in MyAssembly assembly (usually the same as the project name), you could set the type name to 因此,如果您的类型在MyInheritClassMyInheritClass ,它位于MyNamespace命名空间和MyAssembly程序MyNamespace (通常与项目名称相同),您可以将类型名称设置为

MyNamespace.MyInheritClass, MyAssembly

and it should work. 它应该工作。

Also, you should avoid using strings for type names, using the actual Type objects is much safer. 此外,您应该避免使用字符串作为类型名称,使用实际的Type对象会更安全。 TypeToLoad should be of type Type , not string . TypeToLoad应为Type ,而不是string

Or, maybe, you shouldn't use a design like this at all. 或者,也许,你根本不应该使用这样的设计。 I don't know why are you doing this, but I'm pretty sure there is a better solution that doesn't involve setting static fields on a base class like this. 我不知道你为什么这样做,但我很确定有一个更好的解决方案,不涉及在这样的基类上设置静态字段。 (What happens when you have two types that implement the same base class?) (如果有两种类型实现相同的基类,会发生什么?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM