Question

I have this in my code:

Assembly assembly = Assembly.LoadFile(dllFile);
//dllFile has the correct value of a path of a .dll file
foreach (Type type in assembly.GetTypes()) {...}

When I debug my program, everything works fine. When I create the exe for my program, the code comes to assembly.GetTypes() and stops executing. What could cause assembly.GetTypes() to work differently when I'm debugging from when I'm using the exe?

Was it helpful?

Solution

Take a look at this link:

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/3bdaf65c-520c-4a1a-a825-fc2ca2957bf3/

You should never use Assembly.LoadFile(). Use LoadFrom() if you know where the assembly is located, use Load() to let .NET figure out where the assembly is located. Using Load() should be your preference but may require a .config file to help .NET find the assembly

credit to @HansPassant

OTHER TIPS

When you create an exe file, you have to make sure that your assemblies are in the correct folder and also that the exe file has access to the folder.

You can subscribe to that event and do a simple Assembly.LoadFrom from a location known to you or even do some fancy loading of your own based on some system you engineer. There are also TypeResolve and ResourceResolve events in the AppDomain that let you handle those aspects of resolving the various dependencies of an AppDomain. For details see this Microsoft forum thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top