0
votes

The project1 has class1 and interface1. Class1 implements the interface1. I have another project that will test this class1 methods using interface1. Now the catch is I have to load the project1.dll dynamically and use the interface methods to make calls to class1 methods. To do this, I am loading the project1.dll using reflection. Now, I get the methodInfo from the interface and before invoking this method, I should create an instance of the class that I will invoke the method. To create a instance of the class using activator.createInstance I need to know the constructor params. Now, these constructor params are of custom type. As I said earlier I have to load the dll's dynamically. So is there a way to get the type from the assembly load? Or any other approach to achieve the above idea? Below is my code.

Assembly assembly = Assembly.LoadFrom(@"D:\Project1.dll");
Type[] typeArray = assembly.GetTypes();
object obj;

//First create the instance of the class
foreach (Type type in typeArray)
{

    if (type.Name == "Class1")
    {

        Type[] types = new Type[4];

        //I am not able to get the below customParams from the loaded assembly.
        //Is there a way to do this. Can this be done without adding reference?
        types[0] = typeof(CustompParam1);
        types[1] = typeof(CustompParam2);
        types[2] = typeof(CustompParam3);
        types[3] = typeof(CustompParam4);

        obj = Activator.CreateInstance(types);
    }
}

//use the instance of the class to invoke the method from the interface
foreach (Type type in typeArray)
{
    if (type.Name == "Interface1")
    {
        MethodInfo[] mInfo = type.GetMethods();
        foreach (MethodInfo mi in mInfo)
        {
            mi.Invoke(obj, null);
        }
    }
}
1
I'm assuming since you don't know about the custom parameter types, your intent is to call the constructor using default values (null for reference-types, 0-equivalents for value-types) for them? - Chris Sinclair
I know what the custom parameter types are. But I cannot add their reference to my project, I have to get these by loading them dynamically. - Virus
You could use Type.GetConstructors and iterate through all available constructors until you get the one you want. EDIT: Once you find it, you can invoke it to create an instance bypassing the Activator.CreateInstance method altogether. - Chris Sinclair
OK, once I get the constructor while invoking it again I need to pass the custom parameters right? - Virus
If you already have instances of the types, pass them in. If you do not but you know they're all reference types, you can pass in nulls. Otherwise you can go through the ConstructorInfo.GetParameters() and for each ParameterInfo you can leverage its ParameterType to create default values (or invoke other constructors) as you need to. - Chris Sinclair

1 Answers

0
votes

You could get the instances for constructor parameters the same way you create your class instance

Find them by type name, the call Activator.CreateInstance for parameter type, and then put them into Activator.CreateInstance of your original class.