1

I see this question a fair amount on SO, and I've followed that advice, but I appear to be doing something wrong. The dll seems to load in fine but the object CreateInstance is returning is null.

I have this dll:

namespace Backfill
{
public class Module : Kernel.Module
{
    public override void ModuleStart()
    {
        //Stuff
    }
}
}

In another DLL with a DIFFERENT namespace

namespace Kernel
{

public abstract class Module
{
    public abstract void ModuleStart();
}



public static void KernelStart()
    {
        string load_dll = @"Path to DLL";

        Assembly test_dll = Assembly.LoadFile(load_dll + ".dll");

        Module test_mod = (Module)test_dll.CreateInstance("Kernel.Module");

        test_mod.ModuleStart();
    }
}

(Module)test_dll.CreateInstance("Kernel.Module"); is returning null, though. Any idea why?

cost
  • 4,420
  • 8
  • 48
  • 80

2 Answers2

4
(Module)test_dll.CreateInstance("Kernel.Module")

This won't work. As you specified, the Module class which you're trying to instantiate (which, I assume, is the one from the first DLL), is not in the Kernel namespace, but in the Backfill namespace. You should therefore have something along these lines:

(Module)test_dll.CreateInstance("Backfill.Module")
antonijn
  • 5,702
  • 2
  • 26
  • 33
2

Kernel.Module is an abstract class. You cannot create an instance of it directly. You need to create an instance of the derived class.

Edit: Since supplying more info:

@Antonijn has the same answer. You need to specify the type that you want directly. BackFill.Module seems like the correct one.

Before Edit:

If you have the same names in multiple assemblies then yo need to use a fully qualified (including assembly name) for the type you want. For exmaple: https://stackoverflow.com/a/2300428/30225 as an possible answer.

Community
  • 1
  • 1
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • 1
    I didn't specify, sorry, the first dll is in a separate namespace and only references the Kernel library in order to inherit Module. In this case then, how would I make an instance of the derived class in the newly loaded DLL? – cost Feb 14 '13 at 21:01