다음 코드와 같이 하면 된다.

 

 

using System.Reflection;

 

namespace test
{
    class CTest
    {
        public double aaa;
        public bool bbb;
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CTest ctest = new CTest();
            ctest.aaa = 55;
            ctest.bbb = true;

            Type type = typeof(CTest);
            FieldInfo[] f = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            for (int i = 0; i < f.Length; i++)
            {
                {
                    Console.WriteLine(f[i].Name);
                    Console.WriteLine(f[i].GetValue(ctest));
                }
            }

            {
                FieldInfo fld = typeof(CTest).GetField("aaa");
                fld.SetValue(ctest, 66);
            }
            {
                FieldInfo fld = typeof(CTest).GetField("bbb");
                fld.SetValue(ctest, false);
            }

            Console.WriteLine(ctest.aaa);
            Console.WriteLine(ctest.bbb);
        }
    }
}

Posted by 마스샘