Click here to Skip to main content
15,900,461 members
Home / Discussions / C#
   

C#

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

                MemberInfo[] listMemberInfoArray = tcObject.GetType().GetMember("m_intList");
                MemberInfo listMemberInfo = listMemberInfoArray[0];
                MethodInfo addMethodInfo = fieldInfo.FieldType.GetMethod("Add");
                object[] elementValue = { 5 };
                addMethodInfo.Invoke(fieldInfo.GetValue(tcObject), elementValue);

                foreach(int currElement in tcObject.m_intList)
                {
                    System.Console.WriteLine("INt List element {0}", currElement);
                }

                Console.WriteLine();
            }

            {
                FieldInfo fieldInfo = tcObject.GetType().
                                        GetField(
                                        "m_lstElement",
                                        BindingFlags.Static |
                                        BindingFlags.Instance |
                                        BindingFlags.NonPublic |
                                        BindingFlags.Public);

                MethodInfo addMethodInfo = fieldInfo.FieldType.GetMethod("Add");
                Object listElementObject = Activator.CreateInstance(fieldInfo.
                                                            FieldType.
                                                            GetGenericArguments()[0]);

                FieldInfo listElementFieldInfo = listElementObject.GetType().
                                        GetField(
                                        "m_element",
                                        BindingFlags.Static |
                                        BindingFlags.Instance |
                                        BindingFlags.NonPublic |
                                        BindingFlags.Public);

                listElementFieldInfo.SetValue(listElementObject, 2);

                object[] elementValue = { listElementObject };
                addMethodInfo.Invoke(fieldInfo.GetValue(tcObject), elementValue);


                foreach (ListElement currListElement in tcObject.m_lstElement)
                {
                    System.Console.WriteLine("List Item value '{0}'", currListElement.m_element);
                }
            }

        }
    }
</listelement></listelement></int></int>

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 
GeneralRe: Can't open solution file, Format Version 9.00 Pin
Alan N16-Oct-08 0:02
Alan N16-Oct-08 0:02 
GeneralRe: Can't open solution file, Format Version 9.00 Pin
Scott Dorman16-Oct-08 8:02
professionalScott Dorman16-Oct-08 8: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
leppie16-Oct-08 8:02
leppie16-Oct-08 8:02 
QuestionUsing ContextMenuStrip Pin
samulong15-Oct-08 16:01
samulong15-Oct-08 16:01 
Questionlook for project specification to test my c# knowledge Pin
parkerproject15-Oct-08 15:11
parkerproject15-Oct-08 15:11 

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.