Monday, November 12, 2012

How to set field value by reflection in C#?

            Class1 result1 = new Class1();
            FieldInfo[] fields = result1.GetType().GetFields(BindingFlags.Public| BindingFlags.Instance);
            foreach (var f in fields)
            {
                f.SetValue(result1, Convert.ChangeType("1", f.FieldType));
            }

No comments:

Post a Comment