xgeoff
April 1st, 2004, 08:42 AM
I am architecting a solution in which I want to be able to define different types or conditions in xml metadata and define in that metadata what class is called to process the type or condition defined.
This is quite easy to do using reflection UNLESS the piece of code you are calling happens to be in another Assembly, in which case things seem to get a bit more complicated.
First off, in order to instantiate a class dynamically, all you really need is the class type, which you can get by passing the fully qualified name of the class.
So if I can get the type as follows:
style #1: dim myType as Type = Type.GetType("myNamespace.myClassName")
I can instantiate the class like so:
Dim myClassInstance As myClass = System.Activator.CreateInstance(myType)
However, if the class is in another assembly, the syntax for obtaining the Type changes to:
style #2: dim myType as Type = Type.GetType("myNamespace.myClassName, myAssemblyName")
No problem, as this is still easily defined as a value in my xml metadata. However, this does not appear to be working.
My Solution consists of 3 different assemblies:
A) --> the dll which contains the core engine that takes the metadata values, creates the instance of the class, and executes it.
B) --> an exe for testing which simply calls public methods of the core engine dll.
C) --> a dll which contains the classes to be called by the metadata. It is important that these reside in a separate dll to support a kind of "plug-in" capability.
Now, using the syntax described above in style #2 I can dynamically instantiate classes in assembly C from assembly B, no problem.
B ----> C
However, when assembly B calls code in assembly A which tries to instantiate (through reflection) a class in assembly C, it fails saying "Could not load type <myclassname> from assembly <myassemblyname>."
B ----> A ----> C
So I tried using the following:
dim myAssembly as Assembly =Assembly.Load("myAssemblyName")
to load the assemby and get the type as follows:
dim myType as Type = myAssembly.GetType("myClassName")
And I get an error message when loading the assembly stating that the assembly was not found, even though all three aforementioned assemblies are all in the same directory.
Anyone have some insight into why this may be happening, or suggestions on how I can get around this?
I am using Visual Studio .NET.
xgeoff
This is quite easy to do using reflection UNLESS the piece of code you are calling happens to be in another Assembly, in which case things seem to get a bit more complicated.
First off, in order to instantiate a class dynamically, all you really need is the class type, which you can get by passing the fully qualified name of the class.
So if I can get the type as follows:
style #1: dim myType as Type = Type.GetType("myNamespace.myClassName")
I can instantiate the class like so:
Dim myClassInstance As myClass = System.Activator.CreateInstance(myType)
However, if the class is in another assembly, the syntax for obtaining the Type changes to:
style #2: dim myType as Type = Type.GetType("myNamespace.myClassName, myAssemblyName")
No problem, as this is still easily defined as a value in my xml metadata. However, this does not appear to be working.
My Solution consists of 3 different assemblies:
A) --> the dll which contains the core engine that takes the metadata values, creates the instance of the class, and executes it.
B) --> an exe for testing which simply calls public methods of the core engine dll.
C) --> a dll which contains the classes to be called by the metadata. It is important that these reside in a separate dll to support a kind of "plug-in" capability.
Now, using the syntax described above in style #2 I can dynamically instantiate classes in assembly C from assembly B, no problem.
B ----> C
However, when assembly B calls code in assembly A which tries to instantiate (through reflection) a class in assembly C, it fails saying "Could not load type <myclassname> from assembly <myassemblyname>."
B ----> A ----> C
So I tried using the following:
dim myAssembly as Assembly =Assembly.Load("myAssemblyName")
to load the assemby and get the type as follows:
dim myType as Type = myAssembly.GetType("myClassName")
And I get an error message when loading the assembly stating that the assembly was not found, even though all three aforementioned assemblies are all in the same directory.
Anyone have some insight into why this may be happening, or suggestions on how I can get around this?
I am using Visual Studio .NET.
xgeoff