locked
Limits of Reflection - Invoke Method of Current Instance Property RRS feed

  • Question

  • I am learning the C# language right now and am working through the topic of Reflection. If I have the following example class:

    public class MyClass { public static List<string> MyList = new List<string>(); public void AddThroughReflection() { Type myType = MyClass.GetType(); var myFieldInfo = myType.GetField("MyList",BindingFlags.Static); var myMethodInfo = myFieldInfo.GetMethod("Add");

    ???? } }

    I get really confused about how I could call the Add method on the actual static property/field using Reflection at this point.  I know, why don't I just call the Add method as normal.  Just trying to see what I can or cannot do with Reflection right now.

    I have read the Invoke method, but it seems I call Invoke and pass it an "instance" object, but do I have to explicitly pass it MyClass.MyList regardless (which of course makes this whole Reflection example I was playing with futile).

    Thoughts?


    • Moved by Jamles Hez Monday, January 26, 2015 2:44 AM
    • Edited by S O'Connor Monday, January 26, 2015 1:26 PM
    Friday, January 23, 2015 7:54 PM

Answers

  • >>I get really confused about how I could call the Add method on the actual static method using Reflection at this point.

    Add is not a static method. It is an instance method of the List<T> class.

    If you want to get a MethodInfo for the Add method of the MyList field you should call the GetMethod method on the type of MyList:

    public void AddThroughReflection()
         {
              Type myType = this.GetType();
              FieldInfo myFieldInfo = myType.GetField("MyList", BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public);
              MethodInfo myMethodInfo = myFieldInfo.FieldType.GetMethod("Add");
    
         } 
    

    >>I have read the Invoke method, but it seems I call Invoke and pass it an "instance" object, but do I have to explicitly pass it MyClass.MyList regardless

    Yes, you must pass a List<T> object. The MethodInfo object basically only provides access to a method's metadata. It doesn't know anything about any object instance. If you want to call the method, you have to specify which particular object you want to call it on:

              MethodInfo myMethodInfo = myFieldInfo.FieldType.GetMethod("Add");
              myMethodInfo.Invoke(MyList, new object[] { "new item,,," });

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by S O'Connor Monday, January 26, 2015 1:28 PM
    Monday, January 26, 2015 1:04 PM

All replies

  • Hi S O'Connor,

    I would like to move your question to C# related forum for a better support, thanks for your understanding.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 26, 2015 2:46 AM
  • Hello,

    >>I get really confused about how I could call the Add method on the actual static method using Reflection at this point.

    If you want to use the List<T> as a filed and use this filed to call its Add method with reflection, you need to change your original code and add some additional code as below:

    Type myType = typeof(Program);
    
                    var myFieldInfo = myType.GetField("MyList", BindingFlags.Static | BindingFlags.Public);
    
                    var type = myFieldInfo.FieldType;
    
                    var methodAdd = type.GetMethod("Add").Invoke(MyList, new object[] { "1" });
    

    The GetMethod is based on the Type class, FieldInfo class does not contains the GetMethod definition.

    So you need to firstly obtain the type the filed represents and then call its Method the type owns.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 26, 2015 7:55 AM
  • That all makes sense, but if I really wanted to generalize the code in the sense that I don't know the exact name of the instance object used in parameter 1 of the Invoke method, is that possible?

    For example, all I might know is the property name (or field name) as a string.

    // Where the instance MyList is, how can I specify that instance just by only having the name "MyList" as a string and nothing more?
    type.GetMethod("Add").Invoke(MyList,new object[ {"1"});
    

    Maybe that's not even possible?
    Monday, January 26, 2015 12:49 PM
  • >>I get really confused about how I could call the Add method on the actual static method using Reflection at this point.

    Add is not a static method. It is an instance method of the List<T> class.

    If you want to get a MethodInfo for the Add method of the MyList field you should call the GetMethod method on the type of MyList:

    public void AddThroughReflection()
         {
              Type myType = this.GetType();
              FieldInfo myFieldInfo = myType.GetField("MyList", BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public);
              MethodInfo myMethodInfo = myFieldInfo.FieldType.GetMethod("Add");
    
         } 
    

    >>I have read the Invoke method, but it seems I call Invoke and pass it an "instance" object, but do I have to explicitly pass it MyClass.MyList regardless

    Yes, you must pass a List<T> object. The MethodInfo object basically only provides access to a method's metadata. It doesn't know anything about any object instance. If you want to call the method, you have to specify which particular object you want to call it on:

              MethodInfo myMethodInfo = myFieldInfo.FieldType.GetMethod("Add");
              myMethodInfo.Invoke(MyList, new object[] { "new item,,," });

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by S O'Connor Monday, January 26, 2015 1:28 PM
    Monday, January 26, 2015 1:04 PM
  • Ooops, thanks for pointing out my typo in original post "...how I could call the Add method on the actual static method using..."  that should have said "how I could call the Add method on the actual static property/field using..." 

    Anyway, thanks for the answers everyone, I think I see the limitation I was wondering about.

    Monday, January 26, 2015 1:28 PM