Click here to Skip to main content
15,913,486 members
Home / Discussions / C#
   

C#

 
QuestionRe: DataBound DataGridView not displaying data? Pin
nelsonpaixao16-Oct-08 12:54
nelsonpaixao16-Oct-08 12:54 
Question"Allow Service to interact with Desktop" Windows Services Pin
Piyush Vaishnav15-Oct-08 20:14
Piyush Vaishnav15-Oct-08 20:14 
AnswerRe: "Allow Service to interact with Desktop" Windows Services Pin
Scott Dorman16-Oct-08 7:55
professionalScott Dorman16-Oct-08 7:55 
AnswerRe: "Allow Service to interact with Desktop" Windows Services Pin
Steve Messer21-Oct-08 6:25
Steve Messer21-Oct-08 6:25 
GeneralRe: "Allow Service to interact with Desktop" Windows Services Pin
aftkak3-May-10 5:25
aftkak3-May-10 5:25 
GeneralRe: "Allow Service to interact with Desktop" Windows Services Pin
mehappycamper9-Jun-11 2:43
mehappycamper9-Jun-11 2:43 
AnswerRe: "Allow Service to interact with Desktop" Windows Services Pin
stan_p21-Dec-10 1:05
stan_p21-Dec-10 1:05 
QuestionUsing reflection modify List<int> type class member...</int> [modified] Pin
chandrap15-Oct-08 17:12
chandrap15-Oct-08 17:12 
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:55 AM

AnswerRe: Using reflection modify List type class member... Pin
Mogyi16-Oct-08 3:33
Mogyi16-Oct-08 3:33 
GeneralRe: Using reflection modify List type class member... Pin
chandrap16-Oct-08 3:49
chandrap16-Oct-08 3:49 
GeneralRe: 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 
GeneralRe: Using reflection modify List type class member... Pin
Mogyi16-Oct-08 4:26
Mogyi16-Oct-08 4:26 
AnswerUsing reflection modify Generic type List<int> class member...</int> Pin
chandrap16-Oct-08 5:50
chandrap16-Oct-08 5:50 
QuestionCan't open solution file, Format Version 9.00 Pin
C#Coudou15-Oct-08 16:24
C#Coudou15-Oct-08 16:24 
AnswerRe: Can't open solution file, Format Version 9.00 Pin
Mycroft Holmes15-Oct-08 16:53
professionalMycroft Holmes15-Oct-08 16:53 
GeneralRe: Can't open solution file, Format Version 9.00 Pin
C#Coudou15-Oct-08 17:01
C#Coudou15-Oct-08 17:01 
GeneralRe: Can't open solution file, Format Version 9.00 Pin
Mycroft Holmes15-Oct-08 17:18
professionalMycroft Holmes15-Oct-08 17:18 
GeneralRe: Can't open solution file, Format Version 9.00 Pin
C#Coudou15-Oct-08 18:15
C#Coudou15-Oct-08 18:15 
GeneralRe: Can't open solution file, Format Version 9.00 Pin
Mycroft Holmes15-Oct-08 20:02
professionalMycroft Holmes15-Oct-08 20:02 
GeneralRe: Can't open solution file, Format Version 9.00 Pin
Scott Dorman16-Oct-08 7:59
professionalScott Dorman16-Oct-08 7:59 
GeneralRe: Can't open solution file, Format Version 9.00 Pin
Scott Dorman16-Oct-08 7:58
professionalScott Dorman16-Oct-08 7:58 
GeneralRe: Can't open solution file, Format Version 9.00 Pin
leppie15-Oct-08 19:59
leppie15-Oct-08 19:59 
GeneralRe: Can't open solution file, Format Version 9.00 Pin
Mycroft Holmes15-Oct-08 20:05
professionalMycroft Holmes15-Oct-08 20:05 
GeneralRe: Can't open solution file, Format Version 9.00 Pin
C#Coudou15-Oct-08 20:29
C#Coudou15-Oct-08 20:29 

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.