SA Developer .NET

Welcome to SA Developer .NET Sign in | Join | Help
in Search

Types

Last post 08-21-2008, 9:07 by MaLio. 1 replies.
Sort Posts: Previous Next
  •  08-21-2008, 8:05 14176

    Types

    The code below tells me that the type of the two objects differ, even though the types are both "ClassLib.Foo".

    The project references the .dll to create the x1.

    The project's .exe is not in C:\dev\tmp.


                    object x1 = new ClassLib.Foo();
                    object x2 = Activator.CreateInstanceFrom(@"C:\dev\tmp\ClassLib.dll", "ClassLib.Foo").Unwrap();

                    Console.WriteLine(x1.GetType() == x2.GetType() ? "Same type" : "Different types");
                    Console.WriteLine("x1 : {0}", x1.GetType().ToString());
                    Console.WriteLine("x2 : {0}", x2.GetType().ToString());

    If I instantiate x2 from the same physical file that is referenced by the project (ie. the one in the same folder as the .exe) the types are the same.

    WTF?


    "Only the savage fears what he does not understand" - The Silver Surfer
  •  08-21-2008, 9:07 14177 in reply to 14176

    Re: Types

    Indeed .. this is by design, to ensure the integrity of the running application.

    Dynamically loaded assemblies that are not strongly named are loaded (generally) into a different load context (or binding context), so the types within will differ. There are a couple of subtelties between Load and LoadFrom derivatives, I recommend you read Suzanne Cook's blog on the subject (http://blogs.msdn.com/suzcook/).

     

    MaLio

View as RSS news feed in XML
Powered by Community Server (Commercial Edition), by Telligent Systems