views:

2113

answers:

5

What I have now (which successfully loads the plug-in) is this:

Assembly myDLL = Assembly.LoadFrom("my.dll");
IMyClass myPluginObject = myDLL.CreateInstance("MyCorp.IMyClass") as IMyClass;

This only works for a class that has a constructor with no arguments. How do I pass in an argument to a constructor?

+2  A: 

You can with Activator.CreateInstance

Darren Kopp
+1  A: 

Activator.CreateInstance takes a Type and whatever you want to pass to the Types constructor.

http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx

FlySwat
A: 

call

public object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)

instead. MSDN Docs

EDIT: If you are going to vote this down, please give insight into why this approach is wrong/or not the best way.

MagicKat
+6  A: 

You cannot. Instead use Activator.CreateInstance as shown in the example below (note that the Client namespace is in one DLL and the Host in another. Both must be found in the same directory for code to work.)

However, if you want to create a truly pluggable interface, I suggest you use an Initialize method that take the given parameters in your interface, instead of relying on constructors. That way you can just demand that the plugin class implement your interface, instead of "hoping" that it accepts the accepted parameters in the constructor.

using System;
using Host;

namespace Client
{
    public class MyClass : IMyInterface
    {
        public int _id;
        public string _name;

        public MyClass(int id,
            string name)
        {
            _id = id;
            _name = name;
        }

        public string GetOutput()
        {
            return String.Format("{0} - {1}", _id, _name);
        }
    }
}


namespace Host
{
    public interface IMyInterface
    {
        string GetOutput();
    }
}


using System;
using System.Reflection;

namespace Host
{
    internal class Program
    {
        private static void Main()
        {
            //These two would be read in some configuration
            const string dllName = "Client.dll";
            const string className = "Client.MyClass";

            try
            {
                Assembly pluginAssembly = Assembly.LoadFrom(dllName);
                Type classType = pluginAssembly.GetType(className);

                var plugin = (IMyInterface) Activator.CreateInstance(classType,
                                                                     42, "Adams");

                if (plugin == null)
                    throw new ApplicationException("Plugin not correctly configured");

                Console.WriteLine(plugin.GetOutput());
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.ToString());
            }
        }
    }
}
Magnus Akselvoll
A: 

You can also not use Activator.CreateInstance, which could perform better. See below StackOverflow question.

http://stackoverflow.com/questions/2024435/how-to-pass-ctor-args-in-activator-createinstance

thames