1. 程式人生 > >C# 變數名利用反射轉成字串

C# 變數名利用反射轉成字串

using System;
using UnityEngine;

public class TestVar : MonoBehaviour
{
    void Start()
    {
        bool ad = false;
        uint abc = 123;
        Debug.Log(GetVarName<uint>(p => abc));
    }

    public static string GetVarName<T>(System.Linq.Expressions.Expression<Func<T, T>> exp)
    {
        return ((System.Linq.Expressions.MemberExpression)exp.Body).Member.Name;
    }
}

參考他人例子,然後修改成泛型