问一个C#中函数方法调用的问题 请问各位高手,能否有什么方法使得我在只知道类中某些方法的名字时,动态的调用这些方法?例如: Class A中有 一个名字叫 TestFunc()的方法,我只知道“TestFunc”这个字符串的话,如何去调用这个方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 比如有一个类库,包括两个函数,一个带参数,一个不带using System;using System.Reflection;using System.Globalization;namespace Lib{ public class TestLib { private string _returnString; public string ReturnString { get { return _returnString; } set { _returnString = value; } } public TestLib() { Console.WriteLine("Create a new instance"); } public TestLib(string str) { _returnString = str; } public void FunctionA(string [] paras) { try { string b=null; for (int i=0;i<paras.Length;i++) b+=paras[i]; Console.WriteLine("A--"+System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff")+"---"+b); } catch (Exception ex) { Console.WriteLine("FunctionA: "+ex.ToString()); } } public void FunctionB() { Console.WriteLine("B--"+System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff")); } }} 在程序中引用这个类,然后用反射调用using System;using System.Reflection;using System.Globalization;using Lib;namespace Reflection{ /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { string[] a = new string[3]; a[0] = "ABCDE"; a[1] = "DEFGHI"; a[2] = "3454564675"; Call("FunctionA", a); Call("FunctionB", null); Console.ReadLine(); } static void Call(string function_name, string [] paras) { string b = "Lib.dll"; Assembly a = Assembly.LoadFrom(b); Type t = a.GetType("Lib.TestLib");//("TestLib"); try { //Console.WriteLine(t.FullName); if (paras != null) { MethodInfo m = t.GetMethod(function_name, new Type[] { typeof(string[]) }); Object obj = Activator.CreateInstance(t); Console.WriteLine("------------" + obj.GetType()); m.Invoke(obj, new object[] { paras }); } else { MethodInfo m = t.GetMethod(function_name); Object obj = Activator.CreateInstance(t); Console.WriteLine("------------" + obj.GetType()); m.Invoke(obj, null); } } catch (Exception ex) { Console.WriteLine("Main: "+ex.ToString()); } } }} 有楼上说的那么麻烦吗,给Class A加个空间,你引入这个这个空间,new一个A的变量,就能用它的方法了 再问一下,这里的参数能否是那些复杂的类型,比如SqlTranscation之类的? System.Reflection.MethodBase.GetCurrentMethod().Name namespace Test{ class Program { static void Main(string[] args) { Console.Write(getname()); } static string getname() { return System.Reflection.MethodBase.GetCurrentMethod().Name; } }} To:wcmj(望尘莫及) ( ) 有楼上说的那么麻烦吗,给Class A加个空间,你引入这个这个空间,new一个A的变量,就能用它的方法了现在是只有这个名称,不想直接用示例;比如一个动态配置运行的文件,在不同的情况下用不同的函数,有时用A函数,有时用B函数,或者还有很多的其他函数,我们不能把函数写死了,那样就要根据不同情况来改程序,如果用反射配置下就可以了To:foyuan(暴走零零漆)你的程序是返回调用函数的名称,而LZ的意思是要根据函数名称(字符串)来调用函数。 Reflection,反射就可以了~~可以先看看相关知识~~这个用来搞抽像工厂很爽的~~ wangsaokui(无间道III(终极无间)C#MVP) 的回答很有帮助,我先研究研究谢谢 再问一下,这里的参数能否是那些复杂的类型,比如SqlTranscation之类的?MethodInfo m = t.GetMethod(function_name, new Type[] { typeof(string[]) });这句可以修改为复杂类型,如果是自定义的类型,需要引用类型的定义,否则不知道怎么转 又学到知识了,谢谢wangsaokui(无间道III(终极无间)C#MVP)呵呵 如何根据URL获得网页的html源代码? Bitmap bm = new Bitmap(27000, 27000);显示无效参数 谁给看看这个foreach()语句该如何改? C# 自动接通来电后播放声音 随机数问题(第三次..) .net cs程序里怎末才能读到表里实时更新的数据? c#编程规范 soap扩展客户端设置(解决问题马上结帖) 求!把asp分页类 翻译成 c# 如何判斷一個指定路經的文件是否存在 c#下使用fastreport实例 我的winform程序从datagrid导出到excel老是出错,请高手指点!在线等!
using System;
using System.Reflection;
using System.Globalization;namespace Lib
{
public class TestLib
{
private string _returnString;
public string ReturnString
{
get { return _returnString; }
set { _returnString = value; }
} public TestLib()
{
Console.WriteLine("Create a new instance");
} public TestLib(string str)
{
_returnString = str;
} public void FunctionA(string [] paras)
{
try
{
string b=null;
for (int i=0;i<paras.Length;i++)
b+=paras[i]; Console.WriteLine("A--"+System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff")+"---"+b);
}
catch (Exception ex)
{
Console.WriteLine("FunctionA: "+ex.ToString());
}
} public void FunctionB()
{
Console.WriteLine("B--"+System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff"));
}
}
}
using System.Reflection;
using System.Globalization;
using Lib;namespace Reflection
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string[] a = new string[3];
a[0] = "ABCDE";
a[1] = "DEFGHI";
a[2] = "3454564675"; Call("FunctionA", a);
Call("FunctionB", null);
Console.ReadLine();
} static void Call(string function_name, string [] paras)
{
string b = "Lib.dll";
Assembly a = Assembly.LoadFrom(b); Type t = a.GetType("Lib.TestLib");//("TestLib");
try
{
//Console.WriteLine(t.FullName);
if (paras != null)
{
MethodInfo m = t.GetMethod(function_name, new Type[] { typeof(string[]) });
Object obj = Activator.CreateInstance(t);
Console.WriteLine("------------" + obj.GetType());
m.Invoke(obj, new object[] { paras });
}
else
{
MethodInfo m = t.GetMethod(function_name);
Object obj = Activator.CreateInstance(t);
Console.WriteLine("------------" + obj.GetType());
m.Invoke(obj, null);
}
}
catch (Exception ex)
{
Console.WriteLine("Main: "+ex.ToString());
}
}
}
}
{
class Program
{
static void Main(string[] args)
{ Console.Write(getname());
}
static string getname()
{
return System.Reflection.MethodBase.GetCurrentMethod().Name;
}
}
}
有楼上说的那么麻烦吗,给Class A加个空间,你引入这个这个空间,new一个A的变量,就能用它的方法了
现在是只有这个名称,不想直接用示例;
比如一个动态配置运行的文件,在不同的情况下用不同的函数,有时用A函数,有时用B函数,或者还有很多的其他函数,我们不能把函数写死了,那样就要根据不同情况来改程序,如果用反射配置下就可以了To:foyuan(暴走零零漆)
你的程序是返回调用函数的名称,而LZ的意思是要根据函数名称(字符串)来调用函数。
这个用来搞抽像工厂很爽的~~
谢谢
这句可以修改为复杂类型,如果是自定义的类型,需要引用类型的定义,否则不知道怎么转
呵呵