Groups | Blog | Home
all groups > dotnet clr > august 2005 >

dotnet clr : The CLR behaves a bit incoherent



Kjetil Kristoffer Solberg
8/28/2005 12:00:00 AM
If I statically link to an assembly(MyClassLibrary) and create an instance
of a type that works fine.
If i take the assembly class representing the statically linked one and do a
CreateInstance and cast it
to the correct type that works as well.

However if a do an Assembly.LoadFile on the same assembly and do an
Assembly.CreateInstance
and try to cast it to the correct type the cast will fail.

See this example to get the picture:

//
// This Works...
//
MyClassLibrary.Class1 c1 = new MyClassLibrary.Class1();

System.Reflection.Assembly[] list =
AppDomain.CurrentDomain.GetAssemblies();
object c2 = list[5].CreateInstance("MyClassLibrary.Class1",true);

c1 = c2 as MyClassLibrary.Class1;
// c1 not NULL, it works like it should

//
// This Fails...
//
System.Reflection.Assembly asm =
System.Reflection.Assembly.LoadFile(Application.StartupPath+"\\a\\MyClassLibrary.dll");
if ( asm.FullName.Equals(list[5].FullName) ) {// The just loaded assembly
is the same as the statically linked one
object c = asm.CreateInstance("MyClassLibrary.Class1",true);
c1 = c as MyClassLibrary.Class1;
// c1 is NULL, this does not work like it should
}

Since I operate on the same assembly class both these methods should work.
Could this be a bug in the CLR?

PS! I am aware that if I load the assembly from the same path as the
statically linked one this example works as
it should, but that should not matter in my opinion.


Any comments or explanations welcomed

regards
Kjetil Kristoffer Solberg

Kjetil Kristoffer Solberg
8/29/2005 12:00:00 AM
The assembly comes from a different path, and as such is loaded in the
neither binding-context.
So it obviously does not work.

regards
Kjetil Kristoffer Solberg

[quoted text, click to view]

AddThis Social Bookmark Button