执行用户数字化的功能/方法 [英] Execute a function/method digited by the user
本文介绍了执行用户数字化的功能/方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道,.net中是否有一个类可以在运行时调用函数/方法...
例如:
输入要调用的方法名称:sum(10,20)
我的Ideia的代码示例如下:
I want to know, if in the .net have a class to call a function/method in run time...
e.g:
Enter with a method name to call: sum(10,20)
The code sample, of my Ideia is something like this:
class Program
{
static void sum(int a, int b)
{
Console.WriteLine("{0}", a + b);
}
static void Main(string[] args)
{
Console.Write("Enter with a method name to call: ");
string a = Convert.ToString(Console.ReadLine());
// SAMPLE OF IDEA
ProcessMacro PrM = new ProcessMacro(a);
}
}
所以,有可能这样做吗?
如果是,我该怎么做?
谢谢.
So, It''s possible to do this ?
If yes, how I can do this ?
Thanks.
推荐答案
您可以使用反射:
You can use reflection:
System.Reflection.MemberInfo[] members = this.GetType().GetMembers();
var parms = new object[0];
Type TypeToReflect = Type.GetType("System.Int32");
var EmitObj = System.Activator.CreateInstance(this.GetType(),false);
var x = this.GetType().InvokeMember("TestMethod2", BindingFlags.InvokeMethod, null, EmitObj, parms);
我不是Reflection方面的专家,可能有一种对现有对象执行的方法.
I am not an expert in Reflection, and there may be a way to execute on an existing object.
在过去的一个小时中提出了这一要求.您可以使用CodeDom命名空间执行此操作.
This was asked in the past hour. You use the CodeDom namespace to do this.
这篇关于执行用户数字化的功能/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文