使用反射从dll调用方法后获取返回值 [英] Get return value after invoking a method from dll using reflection

查看:63
本文介绍了使用反射从dll调用方法后获取返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用反射加载 dll 并尝试调用返回 List 的方法.如何调用方法并获取返回值.我试过了,但说entry point not found exception.

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 有异常entry point not found.

values has the exception entry point not found.

推荐答案

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.

这篇关于使用反射从dll调用方法后获取返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆