Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get return value after invoking a method from dll using reflection

Tags:

c#

reflection

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.

like image 872
Virus Avatar asked Mar 27 '13 05:03

Virus


1 Answers

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.

like image 125
coolmine Avatar answered Sep 28 '22 04:09

coolmine