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

.NET (Core and Framework)

 
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 
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 
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>

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 
Answer[Cross Post]Re: Visual c++ runtime error on only framework installed machine [modified] Pin
Scott Dorman14-Oct-08 8:27
professionalScott Dorman14-Oct-08 8:27 
GeneralRe: [Cross Post]Re: Visual c++ runtime error on only framework installed machine Pin
Mark Salsbery14-Oct-08 8:32
Mark Salsbery14-Oct-08 8:32 
GeneralRe: [Cross Post]Re: Visual c++ runtime error on only framework installed machine Pin
Scott Dorman14-Oct-08 8:38
professionalScott Dorman14-Oct-08 8:38 
QuestionDifferent exception handling behavior with and without debugger attached Pin
jpsstavares14-Oct-08 5:37
jpsstavares14-Oct-08 5:37 
QuestionRe: Different exception handling behavior with and without debugger attached Pin
jpsstavares14-Oct-08 23:40
jpsstavares14-Oct-08 23:40 
Question[Content_Types].xml in System.IO.Packaging.ZipPackage Pin
Chapooki14-Oct-08 3:59
Chapooki14-Oct-08 3:59 

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.