using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;namespace ConsoleApplication2
{
    /// <summary>
    /// 委托
    /// </summary>
    /// <param name="s1"></param>
    /// <param name="s2"></param>
    /// <returns></returns>
    //public delegate void ProcessDelegate(object sender, EventArgs e);
    class Program
    {
        static void Main(string[] args)
        {
            /*  第一步执行  */
            Test t = new Test();
            t.on("load");
            Console.Read();        }        public static void t_ProcessEvent(object sender, EventArgs e)
        {
            Test t = (Test)sender;
            t.Text1 = "Hello";
            t.Text2 = "World";
        }    }    public class Test
    {
        private string s1;        public string Text1
        {
            get { return s1; }
            set { s1 = value; }
        }        private string s2;        public string Text2
        {
            get { return s2; }
            set { s2 = value; }
        }        public delegate void ProcessDelegate(object sender, EventArgs e);
        public void on(string sEventName)//, Assembly myassemby
        {
            ProcessEvent += new ProcessDelegate(Program.t_ProcessEvent);
            Console.WriteLine(Process());        }        public event ProcessDelegate ProcessEvent;        void ProcessAction(object sender, EventArgs e)
        {
            if (ProcessEvent == null)
                ProcessEvent += new ProcessDelegate(t_ProcessEvent);
            ProcessEvent(sender, e);
        }        //如果没有自己指定关联方法,将会调用该方法抛出错误
        void t_ProcessEvent(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }        void OnProcess()
        {
            ProcessAction(this, EventArgs.Empty);
        }        public string Process()
        {
            OnProcess();
            return s1 + s2;
        }
    }}请高手帮忙看看,在Main函数里执行的t.on("load")代码会调用Main函数里的t_ProcessEvent方法,但是我不想通过Program.t_ProcessEvent来获取t_ProcessEvent方法,而是想通过t.on("load",t_ProcessEvent())这样把t_ProcessEvent方法当作参数传过去,用委托传是可以,但是有没有其他方法,听说反射可以,但是一下难以理解,请高手们帮帮忙。

解决方案 »

  1.   

    用代理吧:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public delegate int AddDelegate(int a, int b);  //代理
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private int add(int a, int b)  // 代理的一个实现函数
            {
                return a + b;
            }        private int test(AddDelegate adddelegate)
            {
                return adddelegate(1, 2);
            }        private void button1_Click(object sender, EventArgs e)
            {
                AddDelegate addDelegate = add;  // 声明代理并指向一个具体的代理实现函数
                int c = addDelegate(12, 23);    // 调用
                int a = test(addDelegate);
            }            
        }
    }
      

  2.   

    没太看懂,大概了解了你的意思
    t.on("load",t_ProcessEvent())
    你这个表达肯定是错误的
    委托很好用,你为什么不用?
    如果你非要反射的话,也可以,那应该是下面这样吧?
    t.on(string EventName,string methodName);
    把方法名作为字符串发过去public void on(string EventName,string methodName)
            {
                //你是不是想调用名为methodName的方法?
                  this.GetType().GetMethod(methodName).Invoke(this,null);//因为没有参数,所以是null,如果有,就传参数过去        }
      

  3.   

    那如何将传过来的方法this.GetType().GetMethod(methodName).Invoke(this,null);绑定到事件ProcessEvent上呢?
      

  4.   

    public void on(string EventName,string methodName) 
            { 
                //转一下弯嘛
                   
                ProcessEvent +=Call;        } 
    void Call(string methodName)
    {
    this.GetType().GetMethod(methodName).Invoke(this,null);
    }