C# 得到類、函數、調用函數的名稱和Unity3d詳細輸出

以前的寫法:
void Foo(Bar bar)  
{  
    if (bar == null)  
    {  
        throw new ArgumentNullException("bar");  
    }  
}
透過這種方式,就不需要寫死 "bar" 字符串。當我們重命名 bar 時,程式也能正常的反應正確的名稱。
Nameof 可以用於取得:類名、方法名、參數名、屬性(Attribute)名。
例子如下:
public class MyClass  
{  
    public static void Show(int age)  
    {  
        Console.WriteLine(nameof(MyClass)); // 輸出 MyClass 類名  
        Console.WriteLine(nameof(Show)); // 輸出 Show 方法名  
        Console.WriteLine(nameof(age)); // 輸出 age  
        Console.WriteLine(nameof(TestMethodAttribute)) // 輸出 Attribute 名  
    }
}


得到當前類名:類的名稱字符串值
public class MyClass  
{  
    public void Show()  
    {  
        Console.WriteLine(this.GetType().Name); // 輸出"MyClass"
    }
    public static void Set()  
    {  
        Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName); // 如果是靜態方法裏,輸出"MyClass" 
    }  
}  



獲取當前函數名稱
public class MyClass  
{  
    public void Show()  
    {  
        //獲取當前函數名稱
        Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); // 輸出"Show"
    }
}  

獲取類中的方法或者字段或者屬性是誰調用了
public class MyClass  
{  
    void Init()
    {
        Show() ;
    }
    public void Show()  
    {  
        //調用函數的名稱:獲取誰調用的我
        Console.WriteLine(new StackTrace().GetFrame(1).GetMethod()); // 輸出"void Init()"
    }
}

U3d中活動詳細輸出詳細:
public class MyClass : MonoBehaviour
{  
    void Init()
    {
       //非靜態函數版本
             string CallStack = "CallStack:[" + new StackTrace().GetFrame(1).GetMethod() + "] => ";
             string SceneName = " Scene:[" + SceneManager.GetActiveScene().name + "]";
             string GameObjectName = " GameObject:[" + gameObject.name + "]";
             string ClassName = " Class:[" + this.GetType().Name + "]";
             string FunctionName = " Function:[" + System.Reflection.MethodBase.GetCurrentMethod().Name + "]";
             string Log = " Log:[" + "runing" + "]";
             string OutputResult = CallStack + SceneName + GameObjectName + ClassName + FunctionName + Log;
             Debug.Log(OutputResult);
    }
    
    public static void Show(GameObject go) 
    {  
     //靜態函數版本
            string callStack = "CallStack:[" + new StackTrace().GetFrame(1).GetMethod() + "] => ";
            string FileName = " FileName:[" + System.IO.Path.GetFileName(new System.Diagnostics.StackTrace(1, true).GetFrame(0).GetFileName()) + "]";
            string LineNumber = " Line:[" + new System.Diagnostics.StackTrace(1, true).GetFrame(0).GetFileLineNumber().ToString() + "]";
            string sceneName = " Scene:[" + SceneManager.GetActiveScene().name + "]";
            string gameObjectName = " GameObject:[" + go.name + "]";
            string className = " Class:[" + System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName + "]";
            string functionName = " Function:[" + System.Reflection.MethodBase.GetCurrentMethod().Name + "]";
            string log = " Log:[" + "GetComponentNullLogError" + "]";
            string OutputResult = callStack + FileName + LineNumber + sceneName + gameObjectName + className + functionName + log;
            Debug.LogError(OutputResult);
    }
}  

//運行效果:
CallStack:[Void On_EnterFirst_SceneLoaded()] =>  Scene:[EnterFirstScene] GameObject:[LeiGong_01(Clone)] Class:[MoveNavMeshAgent] Function:[Init] Log:[runing]

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章