如何从动态调用的方法获取返回值? [英] How to get return value from a method called dynamically?

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

问题描述

例如,我有以下代码(由用户输入):
10 * 2
我必须将其包含在我的动态源代码中,例如:

For example I have the following code (input by user):
10*2
I have to include it in my dynamic source code like as:

//using ...
"public float Calc() {
return " + myTextBox.Text + "; }"

现在,在构建动态程序集之后,我可以使用Type.InvokeMember调用它,但是我想获取所调用方法返回的结果(与上述输入一样,我想获取值20),能否请您告诉我该如何实现?我们是否必须在不使用Type.InvokeMethod的情况下调用该方法?
您的帮助将不胜感激!
谢谢!

Now after building the dynamic assembly, I can invoke it using Type.InvokeMember, but I want to get the result returned by the method invoked (as with the above input, I want to get the value 20), could you please tell me how to achieve that? Do we have to invoke the method without using Type.InvokeMethod?
Your help would be appreciated!
Thanks!

推荐答案

如果您阅读了文档 [^ ],您可以注意这个

返回值

类型:System.Object
表示被调用成员的返回值的对象
If you read the documentation[^] you may notice this

Return Value

Type: System.Object
An object representing the return value of the invoked member


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

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