Click here to Skip to main content
15,889,216 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
JokeRe: Create new web browser Pin
Paul Conrad16-Oct-08 11:05
professionalPaul Conrad16-Oct-08 11:05 
AnswerRe: Create new web browser Pin
Paul Conrad16-Oct-08 11:03
professionalPaul Conrad16-Oct-08 11:03 
JokeRe: Create new web browser Pin
Mark Salsbery16-Oct-08 11:06
Mark Salsbery16-Oct-08 11:06 
AnswerRe: Create new web browser Pin
Shyam Bharath20-Oct-08 0:25
Shyam Bharath20-Oct-08 0:25 
QuestionFetching member data from the DL within the OU Pin
Vipul Mehta16-Oct-08 6:31
Vipul Mehta16-Oct-08 6:31 
QuestionException : Additional information: Required permissions cannot be acquired Pin
NICE TO MEET15-Oct-08 19:41
NICE TO MEET15-Oct-08 19:41 
AnswerRe: Exception : Additional information: Required permissions cannot be acquired Pin
Thomas Stockwell16-Oct-08 3:42
professionalThomas Stockwell16-Oct-08 3:42 
QuestionUsing reflection modify List<int> type class member...</int> [modified] Pin
chandrap15-Oct-08 17:13
chandrap15-Oct-08 17:13 
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);
                }
            }
        }
    }
</string></int></int></int>
[/CODE]

Thanks
Chandra

modified on Thursday, October 16, 2008 1:56 AM

AnswerRe: Using reflection modify List type class member... Pin
chandrap16-Oct-08 3:54
chandrap16-Oct-08 3:54 
GeneralRe: Using reflection modify List type class member... Pin
chandrap16-Oct-08 4:24
chandrap16-Oct-08 4:24 
AnswerRe: Using reflection modify List type class member... Pin
led mike16-Oct-08 4:41
led mike16-Oct-08 4:41 
GeneralRe: Using reflection modify List type class member... Pin
chandrap16-Oct-08 4:50
chandrap16-Oct-08 4:50 
QuestionRe: Using reflection modify List type class member... Pin
led mike16-Oct-08 5:34
led mike16-Oct-08 5:34 
AnswerUsing reflection modify Generic type List<int> class member...</int> Pin
chandrap16-Oct-08 5:49
chandrap16-Oct-08 5:49 
QuestionSimulation in c# Pin
Sareh khanoom15-Oct-08 5:59
Sareh khanoom15-Oct-08 5:59 
AnswerRe: Simulation in c# Pin
Dave Kreskowiak15-Oct-08 7:08
mveDave Kreskowiak15-Oct-08 7:08 
QuestionDataTable PrimaryKey problem Pin
calhuskerfan14-Oct-08 11:54
calhuskerfan14-Oct-08 11:54 
AnswerRe: DataTable PrimaryKey problem Pin
led mike15-Oct-08 5:01
led mike15-Oct-08 5:01 
GeneralRe: DataTable PrimaryKey problem Pin
calhuskerfan15-Oct-08 9:17
calhuskerfan15-Oct-08 9:17 
QuestionRe: DataTable PrimaryKey problem Pin
led mike15-Oct-08 9:58
led mike15-Oct-08 9:58 
AnswerRe: DataTable PrimaryKey problem Pin
calhuskerfan15-Oct-08 10:03
calhuskerfan15-Oct-08 10:03 
AnswerRe: DataTable PrimaryKey problem Pin
calhuskerfan15-Oct-08 10:17
calhuskerfan15-Oct-08 10:17 
GeneralRe: DataTable PrimaryKey problem Pin
led mike15-Oct-08 10:49
led mike15-Oct-08 10:49 
GeneralRe: DataTable PrimaryKey problem Pin
Wendelius15-Oct-08 11:18
mentorWendelius15-Oct-08 11:18 
QuestionVisual c++ runtime error on only framework installed machine Pin
balu1234514-Oct-08 7:19
balu1234514-Oct-08 7:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.