26

I'm completely new to loading in libraries like this, but here's where I stand:

I have a homemade DLL file it's about as simple as it gets, the class itself and a method. In the home program that loads this library, I have:

Assembly testDLL = Assembly.LoadFile("C:\\dll\\test.dll");

From here, I'm kind of stuck. As far as I know, it's loading it correctly because it gives me errors when I change the name.

What do I do from here? How exactly do I load the class & methods within it?

Thanks.

Irshad
  • 3,071
  • 5
  • 30
  • 51
scrot
  • 1,647
  • 8
  • 24
  • 31
  • is there a particular reason you need to use reflection to dynamically load this dll? if not, then use Lunchy's answer. – Stan R. Jul 06 '09 at 16:14

3 Answers3

33

Use Assembly.GetTypes() to get a collection of all the types, or Assembly.GetType(name) to get a particular type.

You can then create an instance of the type with a parameterless constructor using Activator.CreateInstance(type) or get the constructors using Type.GetConstructors and invoke them to create instances.

Likewise you can get methods with Type.GetMethods() etc.

Basically, once you've got a type there are loads of things you can do - look at the member list for more information. If you get stuck trying to perform a particular task (generics can be tricky) just ask a specific question an I'm sure we'll be able to help.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
12

This is how you can get the classes if you know the type.

Assembly assembly = Assembly.LoadFrom("C:\\dll\\test.dll");

// Load the object
string fullTypeName = "MyNamespace.YourType";

YourType myType = assembly.CreateInstance(fullTypeName);

The full type name is important. Since you aren't adding the .dll you can't do a Using because it is not in your project.

If you want all I would just Jon Skeet answer.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
  • 1
    Sorry if I dig this answer of yours from the grave. I have been wondering if it's possible to access Interfaces the same way. That is, if the dll contains a "MyNamespace.MyInterface", how do I get to it? I.e., in your example "YourType" is known to the dll loader. What if it isn't but I know it and its methods by name, i.e. because they are documented somewhere? – manu3d Sep 22 '13 at 23:42
  • @manu3d Yes you can. Works the same way, just replace your type with the interface. – David Basarab Sep 25 '13 at 02:40
3

If you want to dynamically load an assembly, and then invoke methods from classes therein, you need to perform some form of dynamic invoke.

Check here for basic advice on that.

The only bit missing is how to get the type itself, which can easily be retrieved wth code like this:

foreach (Type t in assemblyToScan.GetTypes())
        {
            if(condition)
                //do stuff
        }

And if you simply want to use the assembly statically (by having the assembly available at compile time), then the answer fom Launcy here on this page is the way to go.

Kenan E. K.
  • 13,955
  • 3
  • 43
  • 48