Reflection
-
I have a following structure
App_Code
MyClass.cs [Company.MyNameSpace.MyClass]
Bin
MyLib.dll [Company.MyLibNameSpace.LibraryClass]From
Company.MyLibNameSpace.LibraryClass
I need to create an instance ofCompany.MyNameSpace.MyClass
, but I don't know Class name until execution (it comes from XML configuration), so I use Reflection for it:Type type = Type.GetType("Company.MyNameSpace.MyClass")
The problem is, that when I load a Type from DLL, it doesn't work (type == null), when I do the same from another .cs file in App_Code folder, everything works fine. Is there any work-around to this problem?
-
I have a following structure
App_Code
MyClass.cs [Company.MyNameSpace.MyClass]
Bin
MyLib.dll [Company.MyLibNameSpace.LibraryClass]From
Company.MyLibNameSpace.LibraryClass
I need to create an instance ofCompany.MyNameSpace.MyClass
, but I don't know Class name until execution (it comes from XML configuration), so I use Reflection for it:Type type = Type.GetType("Company.MyNameSpace.MyClass")
The problem is, that when I load a Type from DLL, it doesn't work (type == null), when I do the same from another .cs file in App_Code folder, everything works fine. Is there any work-around to this problem?
-
I have a following structure
App_Code
MyClass.cs [Company.MyNameSpace.MyClass]
Bin
MyLib.dll [Company.MyLibNameSpace.LibraryClass]From
Company.MyLibNameSpace.LibraryClass
I need to create an instance ofCompany.MyNameSpace.MyClass
, but I don't know Class name until execution (it comes from XML configuration), so I use Reflection for it:Type type = Type.GetType("Company.MyNameSpace.MyClass")
The problem is, that when I load a Type from DLL, it doesn't work (type == null), when I do the same from another .cs file in App_Code folder, everything works fine. Is there any work-around to this problem?
As you dont have reference to Company.MyNameSpace.MyClass, you need to manually read the assembly using Reflection and then using
Activator.CreateInstance
you create the instance of the class. For instance : Assembly assem = Assembly.Load("\folder\MyLib.dll"); object thisObj = assem.CreateInstance("Company.MyNameSpace.MyClass"); For further reading use http://www.c-sharpcorner.com/UploadFile/samhaidar/LateBindingWithReflection09122005053810AM/LateBindingWithReflection.aspx[^]:rose:Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript