Using reflection modify List<int> type class member...</int> [modified]
-
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
-
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
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... :^)
-
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... :^)
-
At compile time I do not know whether the type is List or List. At runtime I should interpret it from the fieldInfo.FieldType.
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"
-
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
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