2009-07-13 39 views
18

Tôi có đoạn mã này điều đó không làm việc:AddEventHandler sử dụng phản ánh

public CartaoCidadao() 
{ 
    InitializeComponent(); 

    object o = WebDAV.Classes.SCWatcher.LoadAssembly(); 
    MethodInfo method = 
     this.GetType().GetMethod("Inserted", 
           BindingFlags.NonPublic | BindingFlags.Instance); 

    EventInfo eventInfo = o.GetType().GetEvent("CardInserted"); 
    Type type = eventInfo.EventHandlerType; 
    Delegate handler = Delegate.CreateDelegate(type, this , method); 

    eventInfo.AddEventHandler(o, handler); 
} 

void Inserted(string readerName, string cardName) 
{ 
    System.Windows.Forms.MessageBox.Show(readerName); 
} 

Các tổ chức sự kiện CardInserted tồn tại trong một tập tin DLL và đối tượng "o" tải OK. Trình xử lý ủy nhiệm có giá trị sau khi có hiệu lực. Tôi chỉ không thể kích hoạt sự kiện.

Trả lời

25

Dưới đây là một ví dụ cho thấy làm thế nào để đính kèm một sự kiện sử dụng phản ánh:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var p = new Program(); 
     var eventInfo = p.GetType().GetEvent("TestEvent"); 
     var methodInfo = p.GetType().GetMethod("TestMethod"); 
     Delegate handler = 
      Delegate.CreateDelegate(eventInfo.EventHandlerType, 
            p, 
            methodInfo); 
     eventInfo.AddEventHandler(p, handler); 

     p.Test(); 
    } 

    public event Func<string> TestEvent; 

    public string TestMethod() 
    { 
     return "Hello World"; 
    } 

    public void Test() 
    { 
     if (TestEvent != null) 
     { 
      Console.WriteLine(TestEvent()); 
     } 
    } 
} 
+3

Mối quan tâm duy nhất của tôi với mã là một đại biểu 'Func <>' không hoàn toàn phù hợp cho các sự kiện. Nếu bạn có nhiều đăng ký cho sự kiện, chỉ một trong số chúng thực sự sẽ tạo ra giá trị trả về, điều này có thể dẫn đến hành vi không xác định của ứng dụng. –

5

Khi bạn nói nó không hoạt động ... những gì sẽ xảy ra? Không có gì? Một ngoại lệ?

Suy nghĩ:

  • là cả hai sự kiện và công chúng xử lý? Nếu không, bạn sẽ cần phải chuyển số BindingFlags phù hợp đến các cuộc gọi GetEvent/GetMethod.
  • có chữ ký của khớp điều khiển không?

Dưới đây là một ví dụ làm việc (lưu ý tôi đang sử dụng một trình xử lý tĩnh, vì thế mà null trong Delegate.CreateDelegate):

using System; 
using System.Reflection; 
class Test 
{ 
    public event EventHandler SomeEvent; 
    public void OnSomeEvent() 
    { 
     if (SomeEvent != null) SomeEvent(this, EventArgs.Empty); 
    } 
    static void Main() 
    { 
     Test obj = new Test(); 
     EventInfo evt = obj.GetType().GetEvent("SomeEvent"); 
     MethodInfo handler = typeof(Test) 
      .GetMethod("MyHandler"); 
     Delegate del = Delegate.CreateDelegate(
      evt.EventHandlerType, null, handler); 
     evt.AddEventHandler(obj, del); 

     obj.OnSomeEvent(); 
    } 

    public static void MyHandler(object sender, EventArgs args) 
    { 
     Console.WriteLine("hi"); 
    } 
} 
+0

Tôi không thấy gì cả. Không có gì xảy ra cả. – pedrofernandes

18

Dưới đây là một ví dụ ngắn nhưng đầy đủ mà không công việc:

using System; 
using System.Reflection; 

class EventPublisher 
{ 
    public event EventHandler TestEvent; 

    public void RaiseEvent() 
    { 
     TestEvent(this, EventArgs.Empty); 
    } 
} 

class Test 
{ 

    void HandleEvent(object sender, EventArgs e) 
    { 
     Console.WriteLine("HandleEvent called"); 
    } 

    static void Main() 
    { 
     // Find the handler method 
     Test test = new Test(); 
     EventPublisher publisher = new EventPublisher(); 
     MethodInfo method = typeof(Test).GetMethod 
      ("HandleEvent", BindingFlags.NonPublic | BindingFlags.Instance); 

     // Subscribe to the event 
     EventInfo eventInfo = typeof(EventPublisher).GetEvent("TestEvent"); 
     Type type = eventInfo.EventHandlerType; 
     Delegate handler = Delegate.CreateDelegate(type, test, method); 

     // Raise the event 
     eventInfo.AddEventHandler(publisher, handler); 
     publisher.RaiseEvent(); 
    } 
} 

Bây giờ, khi bạn nói "Tôi chỉ không thể kích hoạt sự kiện", bạn có ý gì? Bạn không có ý định tự mình nâng cao sự kiện - đó là tùy thuộc vào nhà xuất bản sự kiện để thực hiện điều đó. Tất cả mã bạn đã thực sự trình bày cho chúng tôi có hoạt động không? Nếu vậy, có vẻ như nó không thêm trình xử lý sự kiện đó là vấn đề.

Bạn có thể cung cấp thêm thông tin?

Các vấn đề liên quan