2011-02-17 50 views
0

我在asp.net这种方法克隆我的控制:复制事件要克隆Contorl

public static Control Clone(Control ctrlSource) 
{ 
    Type t = ctrlSource.GetType(); 
    Control ctrlDest = (Control)t.InvokeMember("" , BindingFlags.CreateInstance , null , null , null); 
    foreach(PropertyInfo prop in t.GetProperties()) 
    { 
     if(prop.CanWrite) 
     { 
      if(prop.Name == "ID") 
      { 
       ctrlDest.ID = ctrlSource.ID + "cloned" + Security.Cryptography.Cryptography.generateRandomPrivateKey(5); 
      } 
      else 
      { 
       prop.SetValue(ctrlDest , prop.GetValue(ctrlSource , null) , null); 
      } 
     } 
    }  
    return ctrlDest; 

}

如何设置目的地控制源控件事件(如Click事件)?

回答

0

您无法获得使用反射订阅事件的方法列表(AKA Invocation List)。您可以订阅或取消订阅,但无法获取所有方法的列表。每个控件(或类型)都可以以不同的方式实现。

为什么不解释为什么你想这样做,有人可以建议另一种方式来做到这一点。

+0

我有一个按钮,我想在两个面板中显示它。和我添加事件按钮。有了这段代码,我可以克隆它,但没有事件 – Raika 2011-02-17 12:53:44