Sunday, December 16, 2007

AppDomain CreateInstanceAndUnwrap w/ Visual Studio 2008

I recently converted a project form Visual Studio 2005 to Visual Studio 2008 RTM. I am using AppDomain.CurrentDomain.CreateInstanceAndUnwrap to dynamically create an instance similar to the following

string assembly = "Test.dll";
string type = "Test.MyClass";
MyClass obj = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(assembly, type) as MyClass;

This worked fine in 2005 but gets an exception in 2008 - "could not load file or assembly or one of it's dependancies". Looking at the fusion log, which is now in the exception info window in Studio (very nice), I could see that it was looking for Test.dll.dll, Test.dll.exe. So, the extension is being appended to the end of the assembly name. After removing ".dll" from the name of the assembly, the code works fine.

4 Comments:

Anonymous Anonymous said...

If you add "dll",you should use "CreateInstanceFromAndUnwrap",
or else you use "CreateInstanceAndUnwrap" without a "dll".

April 1, 2009 at 9:08 AM  
Blogger Matt said...

Thanks. I didn't even try that one...good to know.

April 7, 2009 at 4:45 PM  
Anonymous Anonymous said...

Then changed the base directory for the AppDomain used by the test runner from the location of the test assemblies in VS 2005 to the IDE's folder in VS 2008, so the test assemblies can't be resolved as they used to. Using CreateInstanceFromAndUnwrap does the trick indeed.

July 24, 2009 at 8:29 AM  
Blogger Katty said...

Great tool, thank you! Can I use it to parse my dll files http://fix4dll.com/msvcp120_dll from the dynamic library? It would be cool!

August 3, 2016 at 7:44 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home