5
Reply

Using reflection modify List type class member...

Chandra

Chandra

Oct 15 2008 11:08 PM
9.9k
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<int> member.

Following is the code.

[CODE]
    class TestClass
    {
        public int i = 0;

        public int IValue
        {
            get
            {
                return i;
            }
            set
            {
                i = value;
            }

        }
        public List<int> m_intList = new List<int>();
    }
    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",
                                        BindingFlags.Static |
                                        BindingFlags.Instance |
                                        BindingFlags.NonPublic |
                                        BindingFlags.Public);

                // HOW do I add elements to m_intList using the fieldInfo object.
                // I am trying to use reflection to modify values for the members.
// In my actual application I do not know the type i.e. whether it is List<int> or List<String> etc

// I will just have the tcObject. From the tcOBject I will get FieldInfo object corresponding to m_intList. Using this FieldINfo, // I should be able to add or remove elements.

                foreach (int intItem in tcObject.m_intList)
                {
                    System.Console.WriteLine("List Item value '{0}'", intItem);
                }
            }
        }
    }
[/CODE]

Thanks
Chandra

Answers (5)