反射的简单用法

2023-05-17  本文已影响0人  价值投机168

{
class AAA
{
public string name;
}
class Program
{
static void Main(string[] args)
{
AAA a = new AAA();

        Type atype = typeof(AAA);
        Type bt = a.GetType();

        var cc = Activator.CreateInstance(bt);
        FieldInfo fi = bt.GetField("name");
        fi.SetValue(cc, "test");
        Console.WriteLine("Hello World!" + fi.GetValue(cc));
        Console.ReadLine();
    }
}

}

上一篇 下一篇

猜你喜欢

热点阅读