Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Using reflection modify List<int> type class member...</int> [modified]

Using reflection modify List<int> type class member...</int> [modified]

Scheduled Pinned Locked Moved .NET (Core and Framework)
help
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chandrap
    wrote on last edited by
    #1

    Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List member. Following is the code. [CODE]

    class TestClass
    {
        public int i = 0;
    
        public int IValue
        {
            get
            {
                return i;
            }
            set
            {
                i = value;
            }
    
        }
        public List m\_intList = new List();
    }
    class Program
    {
        static void Main(string\[\] args)
        {
            TestClass tcObject = new TestClass();
            tcObject.i = 1;
            tcObject.m\_intList.Add(1);
            tcObject.m\_intList.Add(2);
    
            // Following code modifies the field "I".
            {
                FieldInfo fieldInfo = tcObject.GetType().
                                        GetField(
                                        "i",
                                        BindingFlags.Static |
                                        BindingFlags.Instance |
                                        BindingFlags.NonPublic |
                                        BindingFlags.Public);
    
                fieldInfo.SetValue(tcObject, 2);
    
                System.Console.WriteLine("I value '{0}'", fieldInfo.GetValue(tcObject));
            }
    
            // Following code modifies the IValue property.
            {
                PropertyInfo propertyInfo = tcObject.GetType().
                                        GetProperty(
                                        "IValue",
                                        BindingFlags.Static |
                                    BindingFlags.Instance |
                                    BindingFlags.NonPublic |
                                    BindingFlags.Public);
    
                MethodInfo propertySetMethodInfo =
                                propertyInfo.GetSetMethod(true);
    
                propertySetMethodInfo.Invoke(tcObject, new Object\[\] { 3 });
    
                System.Console.WriteLine("Property IValue '{0}'", tcObject.i);
            }
    
            // Following is the actual problem I am having. I would like to add
            // elements to the List member m\_intList.
            {
                FieldInfo fieldInfo = tcObject.GetType().
                                        GetField(
                                        "m\_intList",
                                        BindingFl
    
    C L 2 Replies Last reply
    0
    • C chandrap

      Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List member. Following is the code. [CODE]

      class TestClass
      {
          public int i = 0;
      
          public int IValue
          {
              get
              {
                  return i;
              }
              set
              {
                  i = value;
              }
      
          }
          public List m\_intList = new List();
      }
      class Program
      {
          static void Main(string\[\] args)
          {
              TestClass tcObject = new TestClass();
              tcObject.i = 1;
              tcObject.m\_intList.Add(1);
              tcObject.m\_intList.Add(2);
      
              // Following code modifies the field "I".
              {
                  FieldInfo fieldInfo = tcObject.GetType().
                                          GetField(
                                          "i",
                                          BindingFlags.Static |
                                          BindingFlags.Instance |
                                          BindingFlags.NonPublic |
                                          BindingFlags.Public);
      
                  fieldInfo.SetValue(tcObject, 2);
      
                  System.Console.WriteLine("I value '{0}'", fieldInfo.GetValue(tcObject));
              }
      
              // Following code modifies the IValue property.
              {
                  PropertyInfo propertyInfo = tcObject.GetType().
                                          GetProperty(
                                          "IValue",
                                          BindingFlags.Static |
                                      BindingFlags.Instance |
                                      BindingFlags.NonPublic |
                                      BindingFlags.Public);
      
                  MethodInfo propertySetMethodInfo =
                                  propertyInfo.GetSetMethod(true);
      
                  propertySetMethodInfo.Invoke(tcObject, new Object\[\] { 3 });
      
                  System.Console.WriteLine("Property IValue '{0}'", tcObject.i);
              }
      
              // Following is the actual problem I am having. I would like to add
              // elements to the List member m\_intList.
              {
                  FieldInfo fieldInfo = tcObject.GetType().
                                          GetField(
                                          "m\_intList",
                                          BindingFl
      
      C Offline
      C Offline
      chandrap
      wrote on last edited by
      #2

      At compile time I do not know whether the type is List or List. At runtime I should interpret it from the fieldInfo.FieldType.

      C 1 Reply Last reply
      0
      • C chandrap

        At compile time I do not know whether the type is List or List. At runtime I should interpret it from the fieldInfo.FieldType.

        C Offline
        C Offline
        chandrap
        wrote on last edited by
        #3

        I even tried the following code

                    MethodInfo addMethodInfo = fieldInfo.FieldType.GetMethod("Add"); 
                    object\[\] intValue = { 5 }; 
                    addMethodInfo.Invoke(tcObject, new Object\[\] { 5 }); 
        

        But the above code gives me error "Unhandled Exception: System.Reflection.TargetException: Object does not match target type." when executing the "invoke"

        1 Reply Last reply
        0
        • C chandrap

          Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List member. Following is the code. [CODE]

          class TestClass
          {
              public int i = 0;
          
              public int IValue
              {
                  get
                  {
                      return i;
                  }
                  set
                  {
                      i = value;
                  }
          
              }
              public List m\_intList = new List();
          }
          class Program
          {
              static void Main(string\[\] args)
              {
                  TestClass tcObject = new TestClass();
                  tcObject.i = 1;
                  tcObject.m\_intList.Add(1);
                  tcObject.m\_intList.Add(2);
          
                  // Following code modifies the field "I".
                  {
                      FieldInfo fieldInfo = tcObject.GetType().
                                              GetField(
                                              "i",
                                              BindingFlags.Static |
                                              BindingFlags.Instance |
                                              BindingFlags.NonPublic |
                                              BindingFlags.Public);
          
                      fieldInfo.SetValue(tcObject, 2);
          
                      System.Console.WriteLine("I value '{0}'", fieldInfo.GetValue(tcObject));
                  }
          
                  // Following code modifies the IValue property.
                  {
                      PropertyInfo propertyInfo = tcObject.GetType().
                                              GetProperty(
                                              "IValue",
                                              BindingFlags.Static |
                                          BindingFlags.Instance |
                                          BindingFlags.NonPublic |
                                          BindingFlags.Public);
          
                      MethodInfo propertySetMethodInfo =
                                      propertyInfo.GetSetMethod(true);
          
                      propertySetMethodInfo.Invoke(tcObject, new Object\[\] { 3 });
          
                      System.Console.WriteLine("Property IValue '{0}'", tcObject.i);
                  }
          
                  // Following is the actual problem I am having. I would like to add
                  // elements to the List member m\_intList.
                  {
                      FieldInfo fieldInfo = tcObject.GetType().
                                              GetField(
                                              "m\_intList",
                                              BindingFl
          
          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          chandrap wrote:

          I am trying to modify class instance members using reflection.

          Why?

          led mike

          C 1 Reply Last reply
          0
          • L led mike

            chandrap wrote:

            I am trying to modify class instance members using reflection.

            Why?

            led mike

            C Offline
            C Offline
            chandrap
            wrote on last edited by
            #5

            Its a requirement in my application. Given an instance of class and values for the members, we should be able to update the members with the corresponding values.

            L 1 Reply Last reply
            0
            • C chandrap

              Its a requirement in my application. Given an instance of class and values for the members, we should be able to update the members with the corresponding values.

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              chandrap wrote:

              Given an instance of class and values for the members, we should be able to update the members with the corresponding values.

              Where in there is the requirement to use Reflection?

              led mike

              C 1 Reply Last reply
              0
              • L led mike

                chandrap wrote:

                Given an instance of class and values for the members, we should be able to update the members with the corresponding values.

                Where in there is the requirement to use Reflection?

                led mike

                C Offline
                C Offline
                chandrap
                wrote on last edited by
                #7

                Hello everyone, thanks for your help. I was able to find the solution for my requirement. Following is the code

                class ListElement
                {
                    public ListElement()
                    {
                        m\_element = 0;
                    }
                
                    public ListElement(int element)
                    {
                        m\_element = 1;
                    }
                
                    public int m\_element;
                }
                
                class TestClass
                {
                    public int i = 0;
                
                    public int IValue
                    {
                        get
                        {
                            return i;
                        }
                        set
                        {
                            i = value;
                        }
                
                    }
                    public List m\_intList = new List();
                    public List m\_lstElement = new List();
                }
                class Program
                {
                    static void Main(string\[\] args)
                    {
                        TestClass tcObject = new TestClass();
                        tcObject.i = 1;
                        tcObject.m\_intList.Add(1);
                        tcObject.m\_intList.Add(2);
                        tcObject.m\_lstElement.Add(new ListElement(1));
                
                        // Following code modifies the field "I".
                        {
                            FieldInfo fieldInfo = tcObject.GetType().
                                                    GetField(
                                                    "i",
                                                    BindingFlags.Static |
                                                    BindingFlags.Instance |
                                                    BindingFlags.NonPublic |
                                                    BindingFlags.Public);
                
                            fieldInfo.SetValue(tcObject, 2);
                
                            System.Console.WriteLine("I value '{0}'", fieldInfo.GetValue(tcObject));
                        }
                
                        // Following code modifies the IValue property.
                        {
                            PropertyInfo propertyInfo = tcObject.GetType().
                                                    GetProperty(
                                                    "IValue",
                                                    BindingFlags.Static |
                                                BindingFlags.Instance |
                                                BindingFlags.NonPublic |
                                                BindingFlags.Public);
                
                            MethodInfo propertySetMethodInfo =
                                            propertyInfo.GetSetMethod(true);
                
                            propertySetMethodInfo.Invoke(tcObject, new Object\[\] { 3 });
                
                            System.Console.WriteLine("Property IValue '{0}'", tcObject.i);
                        }
                
                        {
                            FieldInfo fieldInfo = tcObject.GetType
                
                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups