1. 程式人生 > >利用字串獲取變數名裡的值

利用字串獲取變數名裡的值

public class test:MonoBehaviour
{
     string str = "id";
    public int num = 3;
    test2 te=new test2();
    private void Start()
    {
        


        int myNum = (int)te.GetType().GetField(str).GetValue(te);//獲取te的型別,獲取裡面str字串對應的資訊,獲取te物件裡面的這個值。
        myNum += num;
        print(myNum);//輸出為103;
      

    }



}
public class test2 
{
    public int id=100;

}