函数调用 [英] Function call
问题描述
我有问题.
例子我在c#中有一个功能:
I have a problem.
Example I have a funtion in c#:
private void Data_show()
{
....
}
我想调用它,但不能正常调用:Data_show()
我要在调用时使用与字符串相同的函数名称:
I want call it but not call normal is:Data_show()
I want use string same function name for call, when I call:
Form_load(....)
{
string call_funtion="Data_show"
}
推荐答案
我认为您的意思是:如何使用反射来调用方法?
这是一个演示此方法的示例:
Hi,
I think what you mean is: How do I use reflection to call a method?
Here is an example demonstrating the approach:
using System;
using System.Reflection;
namespace Reflect
{
class Program
{
static void Main(string[] args)
{
string strTypeName = "Reflect.Program";
string strMethodName = "Method";
// Use reflection to obtain the type and method infos
Assembly assembly = Assembly.GetExecutingAssembly();
Type type = assembly.GetType(strTypeName);
MethodInfo method = type.GetMethod(strMethodName);
// Call the method by creating an instance via reflection.
Object objInstance = Activator.CreateInstance(type);
method.Invoke(objInstance, null);
Console.ReadKey();
}
public void Method()
{
Console.WriteLine("I''m a method");
}
}
}
但是停下来!首先考虑一下为什么要这样做.问这个问题不是一个好兆头,这不是您的现实世界问题的解决方案; P不,开个玩笑,告诉我们您确切想做什么,也许比使用反射更好的方法...
But stop! First think about why you want to do something like this. Asking this question is not a good sign this is a solution for your real world problem ;P No, joke aside, tell us what you exactly want to do, maybe there is a better aproach than using reflection...
您问如何为方法赋予参数,函数调用(更新) [ ^ ]我改进了上面的示例
you asked how to give parameters to the method, Function call (update)[^]I improved the above example
using System;
using System.Reflection;
namespace Reflect
{
class Program
{
static void Main(string[] args)
{
string strTypeName = "Reflect.Program"; // full type name!
string strMethodName = "Method";
string strMethodWithParamsName = "MethodWithParams";
// Use reflection to obtain the type and method infos
Assembly assembly = Assembly.GetExecutingAssembly();
Type type = assembly.GetType(strTypeName);
MethodInfo method = type.GetMethod(strMethodName);
MethodInfo methodWithParams = type.GetMethod(strMethodWithParamsName);
// Call the methods by creating an instance via reflection.
Object objInstance = Activator.CreateInstance(type);
// ... method
method.Invoke(objInstance, null);
// ... method with parameters
object[] aobjParams = new object[] { "bla", 5 };
methodWithParams.Invoke(objInstance, aobjParams);
Console.ReadKey();
}
public void Method()
{
Console.WriteLine("I''m a method");
}
public void MethodWithParams(string strParam, int iParam)
{
Console.WriteLine("{0}, {1}", strParam, iParam);
}
}
}
但是,现在深吸一口气,您对反射一无所知,而是想使用它-可能只是在教程或文档(msdn)中阅读了基础知识.我想提供帮助,但是如果您使用这样的示例,它将为您带来最大的帮助:
哦-我不需要/使用/听说过/等的新事物.之前.
哦-解决了我的问题
哦-我想进一步了解它!
哦-...互联网...
哦-啊哈!
哦-也许我应该用我的见解帮助别人:酷:
But now take a deep breath, you don''t know about reflection and want to use it - maybe just read the basics in a tutorial or docu (msdn). I like to help but it will help you most if you use my example like this:
Oh - a new thing I didn''t need/use/heard of/etc. before.
Oh - solves my problem
Oh - I want to know more about it!
Oh - ... the internet ...
Oh - Aha!
Oh - maybe I should help others with my insights :cool:
这篇关于函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!