Click here to Skip to main content
16,011,538 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Developers,

I have an assembly and called AssemblyRefTest.dll and this is refereed in my project called AssemblyRefClient. AssemlyRefTest assembly is having class named as AssemblyClass.cs and i want to created object of that class using reflection like below.
C#
var classType = Type.GetType("AssemblyRefTest.AssemblyClass");

but the classType variable is null after this is executed.

and i can access the class directly in my project like below.

C#
AssemblyClass classType = new AssemblyClass();


and i have placed the same class AssemblyClass.cs in my project this time i can created object of that class using reflection it will work fine.

in middle have used this to load assemly just for try.

C#
AppDomain appdomain = AppDomain.CurrentDomain;
            appdomain.Load("AssemblyRefTest");


then also it is not working.

Pleas any one help me to solve this issue.

Thanks & Regards,
V_R
Posted
Comments
Sergey Alexandrovich Kryukov 14-Feb-14 0:56am    
There is no such concept as "call assembly". You either reference it or load by your code during runtime. If you reference it, you don't need to get the type. So far, this question does not make a whole lot of sense. Maybe, start with explaining your goals.
—SA
V_R 14-Feb-14 1:09am    
Hi Sergey,

The assembly which i have referenced is having multiple classes and some classes are using directly by creating new instance and accessing methods inside that, but some scenario i will not be knowing which class has to create and which method has to access and i will get this class name and method name in string formate in runtime so i want to create type of that class and access method using reflection.

Thanks.
Sergey Alexandrovich Kryukov 14-Feb-14 1:11am    
Why? You can use Reflection... but why?
It smells like huge abuse...
—SA

1 solution

C#
////////get class name from database or any other source
  string ClassName = "AssemblyRefTest.AssemblyClass";
  if (ClassName != string.Empty)
  {     
     //////////check which dll has the class
          if (ClassName.StartsWith("AssemblyRefTest"))
           {
               //////////////load classes from dll
               System.Reflection.Assembly assembly =                    System.Reflection.Assembly.LoadFile(Application.StartupPath + "\\AssemblyRefTest.dll");
               //////////get type of class
               Type tp = assembly.GetType(ClassName);
             ///////////////use Activator.CreateInstance(tp)  For creating objects of the Class
             }
             else  if (ClassName.StartsWith("anotherdll.dll"))
                      {
                         ////////  use nested else if ,for loading from multiple dll's
                        }
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900