Can't obtain an instance of Type class for a type not defined in the currently executing assembly
How do I obtain an instance of a Type
class for a type not defined in the currently executing assembly or in mscorlib.dll
?
a) Namely, I've defined a class type someType
in assembly CSharpSnapIn.dll
, located at E:\CSharpSnapIn.dll
, but for some reason when I try to specify an absolute path to this assembly, I get an exception:
Type t = Type.GetType("someType, E:\\CSharpSnapIn.dll"); // exeception
b) I've also tried by putting CSharpSnapIn.dll
into \bin\debug
directory of a currently running application, but I still get an exception:
Type t = Type.GetType("someType, CSharpSnapIn.dll"); // exeception
thanx
EDIT:
1) I've declared another c开发者_如何学Class type someType2
( inside CsharpSnapIn.dll
)and this time it worked:
Type.GetType("someType2, CSharpSnapIn");
Difference between someType
and someType2
is that someType
implements an interface declared in external assembly asmIn
, but this shouldn't cause an exception, since CsharpSnapIn.dll
does have a reference to asmIn
?!
2)
Note that the assembly doesn't need to be loaded first, so long as the assembly resolver can find it
In other words, calling Type.GetType()
first loads an assembly and then creates a Type
instance?
3)
The assembly has to be found by probing, so it would have to be in the bin directory as per your second example. If it's an assembly with a strong name, you have to give all the details.
So you're saying we can't specify an absolute path ( to an assembly ) using Type.GetType()
, but instead assembly needs to reside inside a bin
directory?
You will need to first load the assembly:
Type t = Assembly
.LoadFrom(@"e:\CSharpSnapIn.dll")
.GetType("SomeNs.SomeType", true);
You need to give the assembly name - not the file which contains it.
For example:
Type t = Type.GetType("someType, CSharpSnapIn");
The assembly has to be found by probing, so it would have to be in the bin directory as per your second example. If it's an assembly with a strong name, you have to give all the details. Note that someType
here also has to be fully qualified in terms of the namespace.
Note that the assembly doesn't need to be loaded first, so long as the assembly resolver can find it. For example, if the assembly is in the same directory as the currently executing assembly, that will be fine in most cases.
As Darin says, an alternative is to load the assembly directly - although in my experience there are quite a few "gotchas" in loading assemblies explicitly, particularly if you've got two assemblies in different locations which both rely on a third assembly. Making sure you only get that third assembly loaded once can be tricky.
You need to choose between LoadFile
and LoadFrom
etc. Here are some remarks from MSDN:
Use the LoadFile method to load and examine assemblies that have the same identity, but are located in different paths. LoadFile does not load files into the LoadFrom context, and does not resolve dependencies using the load path, as the LoadFrom method does. LoadFile is useful in this limited scenario because LoadFrom cannot be used to load assemblies that have the same identities but different paths; it will load only the first such assembly.
精彩评论