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. C#
  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 C#
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
    
    M C 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
      
      M Offline
      M Offline
      Mogyi
      wrote on last edited by
      #2

      I don't want to lower your morale but I've been trying some time ago a similar thing to do and I had to abandon the idea because it get's very problematic when you are trying to generalize the issue. The main idea is, if you don't know the 'assembly type', you don't have a chance... :^)

      C 1 Reply Last reply
      0
      • M Mogyi

        I don't want to lower your morale but I've been trying some time ago a similar thing to do and I had to abandon the idea because it get's very problematic when you are trying to generalize the issue. The main idea is, if you don't know the 'assembly type', you don't have a chance... :^)

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

        What do you mean by 'assembly type'. The assembly is .NET assembly written in C#.

        C M 2 Replies Last reply
        0
        • C chandrap

          What do you mean by 'assembly type'. The assembly is .NET assembly written in C#.

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

          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
            #5

            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

              What do you mean by 'assembly type'. The assembly is .NET assembly written in C#.

              M Offline
              M Offline
              Mogyi
              wrote on last edited by
              #6

              Ok, forget 'assembly', if you don't know an Object type, you can't construct it... Hope somebody will have a solution for you problem, I'm just saying what I was trying to do and always came back to the same problem...

              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
                
                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.Get
                
                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