Get return value after invoking a method from dll

2020-07-13 14:17发布

问题:

I am loading a dll using reflection and trying to invoke a method that returns a List<customType>. How do I invoke a method and get the return values. I tried this but says entry point not found exception.

MethodInfo[] info= classType.GetMethods();
MethodInfo method = mInfo.FirstOrDefault(c => c.Name == "GetDetails");
object values = method.Invoke(classInstance, new object[] { param1});

values has the exception entry point not found.

回答1:

Assembly assembly = Assembly.LoadFile(@"assembly location");    // you can change the way you load the assembly
Type type = assembly.GetType("mynamespace.NameOfTheClass");                                       
ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
object classObject = constructor.Invoke(new object[] { });

MethodInfo methodInfo = type.GetMethod("GetDetails");
var returnValue = (List<customType>)methodInfo.Invoke(classObject, new object[] { param1});

A few alterations might be required depending on if your class is static or not and if your constructor takes any parameters.



标签: c# reflection