反射

public class ReflectData
{
    public int id;
    public string name;
}

public class ReflectTest反射
{
    public void test()
    {
        ReflectData data = new ReflectData();
        data.id = 911;
        data.name = "ljc";
             

        System.Type t = typeof(ReflectData);
        FieldInfo info = t.GetField("name");
        object name = info.GetValue(data);
        Debug.Log(name as string); //打印结果为ljc
    }
}

你可能感兴趣的:(反射)