Get return value after invoking a method from dll

2020-07-13 14:07发布

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.

标签: c# reflection
1条回答
兄弟一词,经得起流年.
2楼-- · 2020-07-13 14:51
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.

查看更多
登录 后发表回答