求代码..如何将一个字符串转为一个事件名称?详情请进,,
如何将一个字符串转为一个事件名称,如下代码Button btn=new Button();
btn.Id="mybtn";
btn.Text="点击"
btn.Click+=new EventHandler(btn_click)//btn_click是一个方法名
public void btn_click()
{
//方法代码
}
现在如给这个btn绑定的方法如果是从数据库里取出来的,btn_click就是一个字符串,相当于是
EventHandler("btn_click")这样就是错误的
如何将这个字符串转为一个事件类型呢??好像利用反射可以将字符串动态转为方法名,但不知道如何实现
求高手帖个代码??谢谢! --------------------编程问答--------------------
using System;--------------------编程问答-------------------- 这个好像不能解决楼主的问题? --------------------编程问答-------------------- 这是个参考,你根据这个修改 --------------------编程问答-------------------- 2L是正解,只是楼主你太懒了~
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Customer c = new Customer();
EventInfo evt = c.GetType().GetEvent("OnChange",
BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.Public
);
evt.AddEventHandler(c, new EventHandler(c_OnChange));
c.Change();
Console.Read();
}
static void c_OnChange(object sender, EventArgs e)
{
Console.WriteLine("事件被触发了");
}
}
class Customer
{
public event EventHandler OnChange;
public void Change()
{
if (OnChange != null)
OnChange(this, null);
}
}
}
http://blog.163.com/forever200917@126/blog/static/16998251200782031818498/ --------------------编程问答--------------------
object[] obj = {"haha","lala"};
string FunName = "函数名称";
MethodInfo mi = this.GetType().GetMethod(FunName, BindingFlags.Instance
| BindingFlags.Public | BindingFlags.NonPublic);
if (mi != null)
mi.Invoke(this, obj);
//如果没有参数,obj为null
else
MessageBox.Show("找不到方法");
private void 函数名称(string x,string y)
{
MessageBox.Show(x+y);
}
运行结果怎么是找不到这个方法??? --------------------编程问答--------------------
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load--------------------编程问答-------------------- mEventInfo = GetType(Button).GetEvent("Click")
Dim mEventInfo As Reflection.EventInfo
mEventInfo = GetType(Button).GetEvent("Click")
Dim mMethodInfo As Reflection.MethodInfo
mMethodInfo = Me.GetType.GetMethod("ReadySayHello", Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
Dim mClick As [Delegate]
mClick = [Delegate].CreateDelegate(mEventInfo.EventHandlerType, Me, mMethodInfo)
mEventInfo.AddEventHandler(Me.Button1, mClick)
End Sub
Private Sub ReadySayHello(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Hello")
End Sub
mEventInfo.AddEventHandler(Me.Button1, mClick) --------------------编程问答--------------------
btn.Click += (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), GetType().GetMothed(btn_click, BindingFlags.Instance | BindingFlags.Public));
需要你的btn_click与EventHandler声明匹配。 --------------------编程问答-------------------- 名称呢匹配就可以了 --------------------编程问答-------------------- GetMethod --------------------编程问答--------------------
忘记target了,修改一下
btn.Click += (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), this, GetType().GetMethod(btn_click, BindingFlags.Instance | BindingFlags.Public));--------------------编程问答-------------------- --------------------编程问答-------------------- 已解决,代码给大家
//创建一个按钮
Button newbtn = new Button();
newbtn.ID = "new";
newbtn.Text = "new";
newbtn.Visible = true;
//利用反射将字符串转为事件名称
newbtn.Click += (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), this, GetType().GetMethod("newBtn_click", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
补充:.NET技术 , C#