mardi 10 mars 2020

How to set value using system.refelection

I would like to know, for the following code example, how I can assign values to myClass Obj, using Type.InvokeMember or with field.SetValue. The field name and value are represented as strings.

    class myClass
    {
        public string obj1;
        public string obj2;
    }

    class myClass2
    {
       public myClass obj = new myClass();
    }

    class Program
    {
        static void Main(string[] args)
        {
            Type type = Type.GetType("ConsoleApp2.myClass2");

            object classObj = Activator.CreateInstance(type);
            FieldInfo fi = type.GetField("obj");
            fi.SetValue(??????);
            type.InvokeMember("obj", BindingFlags.SetField, null, classObj , ?????);
        }
    }




Aucun commentaire:

Enregistrer un commentaire