C# Developers' Journal (original) (raw)

Loading assemblies I'm sure I'm missing something simple here. I've created a dll containing a trivial c# class:


namespace Library
{
    public class Foo
    {
        public static string Name()
        {
            return "Foo";
        }
    }
}

Now I'm trying to open it and find the class using reflection. Given that dir is a System.IO.DirectoryInfo with the directory listing in it, the code I'm using to open the dll (I've stripped out the error checking) is:


System.IO.FileInfo[] files = dir.GetFiles("*.dll");
foreach (System.IO.FileInfo fi in files)
{
    string filename = fi.FullName;
                System.Reflection.Assembly a = System.Reflection.Assembly.Load(filename);
    // etc.

But the attempt to load the assembly gives:

System.IO.FileLoadException was unhandled Message="The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)"

According to the help, that means that it could find the file, but it wasn't a valid assembly.

So what do I need to do to make it a valid assembly?