0

Apologies had a typo...have edited...

I have a weird issue I am not sure about.

In one piece of code I have a class which is called as a singleton which has an event other classes can listen to, pretty straightforward by doing something like

Client.Instance.MyEvent += new EventHandler<EventArgs>(myHandler);

So if I have a generic class:

Class MyTest {
   public MyTest() {
       System.Console.WriteLine("In Constructor Registering Events");
       Client.Instance.MyEvent += new EventHandler<EventArgs>(myHandler);
   }
   private void myHandler(object sender, EventArgs arg) {
      System.Console.WriteLine("Got event!");
   }
}

Now if i create the class like:

MyTest mC = new MyTest ();
Client.Instance.FireEvent();

I get the expected "In Constructor Registering Events" and "Got Event"

However if i create the class through Reflection, I do not.

Type mType = typeof(MyTest);
object mT = Activator.CreateInstance(mType);
Client.Instance.FireEvent();

All i get is "In Constructor Registering Events" but i DO NOT get the event fired message. whats going on here? Am i doing something incorrectly in my reflection calls?

Thanks -

user1772250
  • 319
  • 5
  • 15

1 Answers1

0

I've just tested your claim and, with the proper type, it works the same whether the object is created using new or via reflection.

The following Working Demo can be tested here

public class Program
{
    public static void Main(string[] args)
    {                        
        Client.Instance.MyEvent += delegate { Console.WriteLine("MY EVENT handled from Main"); };

        MyTest mt = new MyTest();

        Type mType = typeof(MyTest);
        object reflectedMT = Activator.CreateInstance(mType);

        Client.Instance.FireEvent();
    }
}

public class Client {
    private Client() {}
    private static Client _inst = new Client();
    public static Client Instance { get { return _inst; } }
    public void FireEvent() { if(MyEvent != null) MyEvent(this, EventArgs.Empty); }
    public event EventHandler<EventArgs> MyEvent;
}

public class MyTest {
   public MyTest() {
      System.Console.WriteLine("In Constructor Registering Events");
      Client.Instance.MyEvent += new EventHandler<EventArgs>(myHandler);
   }
   private void myHandler(object sender, EventArgs arg) {
      System.Console.WriteLine("Got event!");
   }
}
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • It's not clear what you mean exactly but you can't invoke methods on the UI from a non-UI thread. If that's what happening you will have to use the Invoke() method of one of your Controls to call your code on the UI thread instead of the other non-UI thread. – Mike Dinescu Mar 13 '13 at 19:32
  • Ah it was my fault, in the client code where "FireEvent" is called, it was actually in a while loop (after i get data off a socket) i was using a delegate to fire the event, and the delegate was initalized outside of the loop, so once it got into the while loop, effectivly even though event += was called it wasnt being registered. all is well now. thanks for the help everyone! – user1772250 Mar 13 '13 at 20:12
  • glad to hear you solved your problem.. you might show your appreciation by voting on the answer if you thought it helped ;) – Mike Dinescu Mar 13 '13 at 20:18