Reputation: 11
I am loading a dll, creating an instance and want to invoke methods and check return value. I am getting an exception {"Parameter count mismatch."} when creating an instance:
static void Main(string[] args)
{
ModuleConfiguration moduleConfiguration = new ModuleConfiguration();
// get the module information
if (!moduleConfiguration.getModuleInfo())
throw new Exception("Error: Module information cannot be retrieved");
// Load the dll
string moduledll = Directory.GetCurrentDirectory() + "\\" +
moduleConfiguration.moduleDLL;
testDLL = Assembly.LoadFile(moduledll);
// create the object
string fullTypeName = "MyNameSpace."+ moduleConfiguration.moduleClassName;
Type moduleType = testDLL.GetType(fullTypeName);
Type[] types = new Type[1];
types[0] = typeof(string[]);
ConstructorInfo constructorInfoObj = moduleType.GetConstructor(
BindingFlags.Instance | BindingFlags.Public, null,
CallingConventions.HasThis, types, null);
if (constructorInfoObj != null)
{
Console.WriteLine(constructorInfoObj.ToString());
constructorInfoObj.Invoke(args);
}
The constructor for the class in dll is:
public class SampleModule:ModuleBase
{
/// <summary>
/// Initializes a new instance of the <see cref="SampleModule" /> class.
/// </summary>
public SampleModule(string[] args)
: base(args)
{
Console.WriteLine("Creating SampleModule");
}
Qs: 1. What am I doing wrong? 2. How do I get method, invoke it and get return values? 3. Is there a better way to do this?
Upvotes: 0
Views: 97
Reputation: 11
Just needed to add the following line:
Object[] param = new Object[1] { args };
before:
constructorInfoObj.Invoke(args);
Alternate (short) solution without using ConstructorInfo:
:
// create the object
string fullTypeName = "MyNameSpace."+ moduleConfiguration.moduleClassName;
Type moduleType = testDLL.GetType(fullTypeName);
Object[] param = new Object[1] { args };
Activator.CreateInstance(runnerType, param);
Upvotes: 1