views:

87

answers:

2

Is there any implications of loading types dynamically from a .NET assembly that is executable (.exe) compared to loading types from an .NET assembly compiled into a .dll?

What is the best and quickest way to test .exe and .dll if it is a .NET executable or not (just not a big fan of the BadImageFormatException)?

Thank you.

+6  A: 

EXE and DLL files aren't much different in Windows, and even less so in .NET. It's essentially just a matter of what the entry point is and a few other details. (.NET DLLs do have an entry point, but you don't implement it. In native code it's called DllMain.)

As for testing if it's a .NET assembly, I'd recommend simply trying to load it for reflection-only and catching the exception. But if you really truly want to avoid that, check out this blog entry.

Promit
Yep. In my experience, the CLR doesn't see any difference between them; they are both assemblies full of types that can be referenced.
Christian Hayter
+1  A: 

There is one important consideration of using an exe to host types... it isn't very well supported by VS2005. The compiler (csc), runtime, etc all see no real problems - simply that the IDE isn't very happy about adding references to exe files.

This is fixed in VS2008, but if you need to support VS2005 developers, I would recommend using a dll for the reference.

At the assembly level, there isn't a huge amount of difference.

For distinguishing between the two (exe/dll) - check for an entry-point on the assembly:

using System;
using System.Reflection;
using System.Net;

class Program {
    static void Main() {
        IsItAnExe(typeof(Program).Assembly); // this one ;-p
        IsItAnExe(typeof(string).Assembly); // mscorlib
        IsItAnExe(typeof(WebClient).Assembly); // System
    }
    static void IsItAnExe(Assembly assembly) {
        MethodInfo entryPoint = assembly.EntryPoint;
        Console.WriteLine(assembly.GetName().Name + ": " +
            (entryPoint == null ? "no entry-point" :
            (entryPoint.DeclaringType.FullName + "." + entryPoint.Name)));
    }
}
Marc Gravell