1

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

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. 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). Here I am getting the Null value in myClass variable. The code I have written in web.config of the main project is

<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.

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?

Rajat
  • 141
  • 1
  • 1
  • 7
  • 1
    It would be more normal to have the class library expose some means for the *consumer* to pass this sort of information through to it and/or provide *factory* methods to it via which you could construct needed objects. Having the class library invert the normal relationships and poke through the consumer's classes seems wrong here. – Damien_The_Unbeliever Sep 18 '18 at 06:33

1 Answers1

2

The particular overload of GetType that you're using states:

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.

But the type that you're trying to load is neither in Mscorlib.dll nor in the current assembly. So you need to supply an assembly qualified name. Something like Child.ChildContacts, MainProject.dll (assuming the assembly is not strong named).

But note my comments that you probably ought to instead have the main project supplying configuration or factory methods explicitly to the class library1 so that it's not trying to pull apart the consuming application via reflection.


1Or embrace Dependency Injection2 and design your class library to assume the presence of one. Then simply list it as a requirement for use of your class library that a suitable Contacts implementation is registered in the container.

2E.g. Dependency injection in ASP.Net Core

Community
  • 1
  • 1
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • +1 for dependency injection - it's exactly what is needed to provide classes to a lib project depending on the start/executing project – nilsK Sep 18 '18 at 07:52