简体   繁体   English

如何通过类库项目中的类获取主项目中类的类型

[英]How to get the Type of a class in Main Project through a class in Class Library Project

I have a class library project which is attached to a main project and in this class library project I have a class named SQSQueueScheduler.cs under this class I have written the below code 我有一个附加到主项目的类库项目,并且在该类库项目中,有一个名为SQSQueueScheduler.cs的类,在该类下我编写了以下代码

Contacts contact = (Contacts)ConfigurationManager.GetSection("bolteContactConfiguration");
 if (contact != null)
        {
            className = contact.ClassName;
            Type myclass = Type.GetType(className);
            objContacts = (Contacts)Activator.CreateInstance(myclass);
        }

The Contacts here is a base class through which I want to create the instnace of derived class ChildContacts dynamically. 这里的Contacts是一个基类,我想通过该基类动态创建派生类ChildContacts的实例。 This base class is present in class library project and the derived class in main website. 该基类在类库项目中存在,派生类在主网站中存在。 The value of variable className is Child.ChildContacts(AIST is the namespace and AISTContacts is the name of class). 变量className的值是Child.ChildContacts(AIST是名称空间,AISTContacts是类的名称)。 Here I am getting the Null value in myClass variable. 在这里,我在myClass变量中获取了Null值。 The code I have written in web.config of the main project is 我在主项目的web.config中编写的代码是

<section name="bolteContactConfiguration" type="TDNBolte.Contacts,3DN Bolte" requirePermission="false"/>
<bolteContactConfiguration name ="Contacts" className="Child.ChildContacts"/>

Here 3DN Bolte is the name of assembly of class library project. 3DN Bolte是类库项目的程序集的名称。

I would like to ask, is there a way to get the type of class present in main website(ChildContacts) through the code written in a class(SQSQueueScheduler) of class library project? 我想问一下,有没有一种方法可以通过类库项目的类(SQSQueueScheduler)中编写的代码来获取主网站(ChildContacts)中存在的类的类型?

The particular overload of GetType that you're using states: 您正在使用GetType的特定重载状态:

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中,则只需提供其名称空间限定的类型名称即可。

But the type that you're trying to load is neither in Mscorlib.dll nor in the current assembly. 但是,您尝试加载的类型既不在Mscorlib.dll也不在当前程序集中。 So you need to supply an assembly qualified name. 因此,您需要提供程序集合格名称。 Something like Child.ChildContacts, MainProject.dll (assuming the assembly is not strong named). 诸如Child.ChildContacts, MainProject.dll类的东西(假定程序集不是强命名的)。

But note my comments that you probably ought to instead have the main project supplying configuration or factory methods explicitly to the class library 1 so that it's not trying to pull apart the consuming application via reflection. 但是请注意我的评论,您可能应该改为让主项目为类库1显式提供配置或工厂方法,以便它不会尝试通过反射将消耗的应用程序拆开。


1 Or embrace Dependency Injection 2 and design your class library to assume the presence of one. 1或者接受Dependency Injection 2,并设计您的类库以假定其中一个存在。 Then simply list it as a requirement for use of your class library that a suitable Contacts implementation is registered in the container. 然后只需将其列出为使用类库的要求,即在容器中注册了合适的Contacts实现。

2 Eg Dependency injection in ASP.Net Core 2例如,ASP.Net Core中的依赖注入

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

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