lundi 26 mars 2018

Assembly.CreateInstance returning null even though I can see the class in DefinedTypes

I use the following method to load a new Assembly and create an instance of a class into a new AppDomain.

private static object CreateInstanceFromBinary(AppDomain appDomain, string typeName)
{
    Assembly entryAssembly = Assembly.GetEntryAssembly();

    byte[] assemblyBinary = LoadAssemblyBinary();

    Assembly loadedAssembly = appDomain.Load(assemblyBinary);
    if (loadedAssembly != null)
    {
        return loadedAssembly.CreateInstance(typeName);
    }

    return null;
}

Which get's called like so.

AppDomain appDomain = AppDomain.CreateDomain(domainName);

appDomainHelper = CreateInstanceFromBinary(appDomain, typeof(MyClass).FullName) as MyClass;

Looking into the loadedAssembly I can see that MyClass exists inside of the DefinedTypes and it's name matches typeName. However, when the code runs

loadedAssembly.CreateInstance(typeName)

it returns null.

This code was working, however, I recently moved this class into the same dll as the one that calls it and now it has started returning null.

Any ideas on how to fix this?





Aucun commentaire:

Enregistrer un commentaire