请教个问题:如何把一个函数名作为参数,调用函数的时候可以通过改变参数,调用不同的函数 请教个问题:如何把一个函数名作为参数,调用函数的时候可以通过改变参数,调用不同的函数 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 C#中的函數可以使用多載技術.即名稱相同, 參數不同.調用時, 系統會依參數個數及類別去自動匹配相就的函數.對於對態調用函數名稱, 可以使用委托比如BUTTON控件的CLICK動態使用函數CLICK()BUTTON.CLICK += NEW EventHandler(CLICK)即可. 對於對態調用函數名稱, 可以使用委托比如BUTTON控件的CLICK動態使用函數CLICK()BUTTON.CLICK += NEW EventHandler(CLICK)即可.对于这种情况,CLICK函数可否重载? 传委托啊这样public delegate void TestHandler();public class Test{ public bool B{get;set;}//08语法 public void Invoke(TestHandler th) { //写些条件啊这些比如: if(B) th.Invoke();//或直接写th();也一样 } static void Main() { Console.WriteLine("test..."); Test t=new Test(); t.B=true; t.Invoke( t.TestM1); t.B=false; t.Invoke(t.TestM1); t.B=true; t.Invoke(t.TestM2); } void TestM1(){Console.WriteLine("hellow world!");} void TestM2(){Console.WriteLine("thank you !");}} 用接口using System;using System.Collections;using System.Reflection;namespace SampleExmp{ interface ItheFun { void theFun1();//方法1 } class class1 : ItheFun { public void theFun1() { System.Console.WriteLine("方法1"); } } class class2 : ItheFun { public void theFun1() { System.Console.WriteLine("方法2"); } } class class3 : ItheFun { public void theFun1() { System.Console.WriteLine("方法3"); } } class class4 : ItheFun { public void theFun1() { System.Console.WriteLine("方法4"); } } class test { static void Main(string[] args) { class1 theClass1 = new class1(); test theTest = new test(); theTest.OutputData(theClass1); } public void OutputData(ItheFun mtheFun) { mtheFun.theFun1(); } }}用迟绑定//csc /t:library /out myTest2.dll 目录/SampleExmp.csusing System;using System.Reflection;namespace theTest001{ class testAssembly { public void UseReflection() { Assembly a = Assembly.LoadFile(@"目录\myTest2.dll"); Type theType = a.GetType("Class1"); object theObj = Activator.CreateInstance(theType); MethodInfo theMethod = theType.GetMethod("theFun1"); Type[] parame = new Type[0]; theMethod.Invoke(theObj, parame); } } class test { static void Main(string[] args) { testAssembly testA = new testAssembly(); testA.UseReflection(); } }}当然委托最好了直接是方法的抽象上面说了,不用重复 急急急~~~~明天交!dataGridView删除问题! 帮看下 这么写对不 超简单 form1.InnerHtml赋值前台不显示 如何用XML动态绑定GridView 这个应该怎么实现,属于分布式应用问题么? 如何使用C#将doc文档转换成txt 菜鸟初到,有一个RadioButton的问题请教各位高手! 今天是我生日,希望CSDN的兄弟们给说几句,散180分 小弟新手,请教个关于OdbcConnection连接怎么写相对路径的问题 在C#中将SQL SERVER 2000数据导入到Excel中 关于继承的问题 菜单功能如何实现 c/s
調用時, 系統會依參數個數及類別去自動匹配相就的函數.對於對態調用函數名稱, 可以使用委托
比如BUTTON控件的CLICK動態使用函數CLICK()
BUTTON.CLICK += NEW EventHandler(CLICK)即可.
比如BUTTON控件的CLICK動態使用函數CLICK()
BUTTON.CLICK += NEW EventHandler(CLICK)即可.对于这种情况,CLICK函数可否重载?
这样public delegate void TestHandler();
public class Test
{
public bool B{get;set;}//08语法
public void Invoke(TestHandler th)
{
//写些条件啊这些比如:
if(B)
th.Invoke();//或直接写th();也一样
}
static void Main()
{
Console.WriteLine("test...");
Test t=new Test();
t.B=true;
t.Invoke( t.TestM1);
t.B=false;
t.Invoke(t.TestM1);
t.B=true;
t.Invoke(t.TestM2);
}
void TestM1(){Console.WriteLine("hellow world!");}
void TestM2(){Console.WriteLine("thank you !");}
}
using System;
using System.Collections;
using System.Reflection;
namespace SampleExmp
{
interface ItheFun
{
void theFun1();//方法1
}
class class1 : ItheFun
{
public void theFun1()
{
System.Console.WriteLine("方法1");
}
}
class class2 : ItheFun
{
public void theFun1()
{
System.Console.WriteLine("方法2");
}
}
class class3 : ItheFun
{
public void theFun1()
{
System.Console.WriteLine("方法3");
}
}
class class4 : ItheFun
{
public void theFun1()
{
System.Console.WriteLine("方法4");
}
}
class test
{
static void Main(string[] args)
{
class1 theClass1 = new class1();
test theTest = new test();
theTest.OutputData(theClass1);
}
public void OutputData(ItheFun mtheFun)
{
mtheFun.theFun1();
}
}
}
用迟绑定
//csc /t:library /out myTest2.dll 目录/SampleExmp.cs
using System;
using System.Reflection;
namespace theTest001
{
class testAssembly
{
public void UseReflection()
{ Assembly a = Assembly.LoadFile(@"目录\myTest2.dll");
Type theType = a.GetType("Class1");
object theObj = Activator.CreateInstance(theType);
MethodInfo theMethod = theType.GetMethod("theFun1");
Type[] parame = new Type[0];
theMethod.Invoke(theObj, parame); }
}
class test
{
static void Main(string[] args)
{
testAssembly testA = new testAssembly();
testA.UseReflection();
}
}
}
当然委托最好了
直接是方法的抽象
上面说了,不用重复