locked
Getting a reference to a class using reflection RRS feed

  • Question

  • Hi, Can anybody help me with how to get the reference to a class already defined so that I can attach an event handler using reflection. I have written a server class which collects data. The server class has an event "NewData". I have created an instance of the class on a form (FrmMain) as private field called dataServer.

    On other Forms  within the application I want to place a client component class which will get the data from the server. The client class will hook onto to "NewData" event using the reflection AddHandler method. However, the AddHandler method requires an object reference to the source (dataServer in FrmMain). I'm not sure how to get the object reference.

    One key requirement is total encapsulation, so I don't want to be declaring public for the server or passing a reference to the server as an argument to the client constructor.

    I have used code to get reference to the dataServer field as shown below:

     

    But I don't know how to get its reference.

            // Get the executing assembly and the data server class.
            this.asm = Assembly.GetExecutingAssembly();
    
            // Find the server instance in the assembly.
            FieldInfo dataSvr = null;
            foreach (Type t in asm.GetTypes())
            {
              foreach (FieldInfo fi in t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
              {
                if (fi.FieldType == typeof(DataServer))
                {
                  // Get reference to instance.
                  dataSvr = fi;
                  break;
                }
              }
              // If data server found break out of outer loop.
              if (dataSvr != null)
              {
                break;
              }
            }
    
            if (dataSvr == null)
            {
              throw new NullReferenceException("No instance of the MoDAS_HD.Comms.DataServer found!");
            }
    
    
    
            object inst ?????// This is where I need the reference
    
    
            // Get the new data event info.
            this.ei_NewData = dataSvr.FieldType.GetEvent("NewData");
    
            delNewData de = new delNewData(dataServer_NewData);
            this.ei_NewData.AddEventHandler(inst, de);
    

    I am trying to achieve this on WM6 platform. Any help would be appreciated.

    Thanks

    Andy

     

    Saturday, October 9, 2010 7:45 PM

Answers

  • AFAIK you can't really get hold on existing instances of the type through reflections, you can only create new instance.

    You also can get value of a private field but you would need a reference to an instance of the class which has that private field.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by andylong0206 Monday, October 11, 2010 6:18 AM
    Sunday, October 10, 2010 5:34 PM

All replies

  • AFAIK you can't really get hold on existing instances of the type through reflections, you can only create new instance.

    You also can get value of a private field but you would need a reference to an instance of the class which has that private field.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by andylong0206 Monday, October 11, 2010 6:18 AM
    Sunday, October 10, 2010 5:34 PM
  • Hi, thanks for that. I've used a singelton class instance of the server and used the get value method with null as the object. I now get a reference to the singleton instance.

    Thanks again.

    Andy

    Monday, October 11, 2010 6:18 AM