呼び出し元のメソッド名、行数、ファイル名の取得

.netcore
https://docs.microsoft.com/ja-jp/dotnet/api/system.runtime.compilerservices.callermembernameattribute?redirectedfrom=MSDN&view=netcore-3.1

using System;

namespace ConsoleAppCS
{
    class Program
    {
        static void Main(string[] args)
        {
            Log("メッセージ1");

            Function1();
        }

        static void Function1()
        {
            Log("メッセージ2");
        }

        static void Log(string message,
        [System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
        [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
        [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
        {
            Console.WriteLine("message: " + message);
            Console.WriteLine("member name: " + memberName);
            Console.WriteLine("source file path: " + sourceFilePath);
            Console.WriteLine("source line number: " + sourceLineNumber);
        }
    }
}


message: メッセージ1
member name: Main
source file path: C:\Users\csharp\source\repos\ConsoleApplication1\ConsoleAppCS\Program.cs
source line number: 9
message: メッセージ2
member name: Function1
source file path: C:\Users\csharp\source\repos\ConsoleApplication1\ConsoleAppCS\Program.cs
source line number: 16

投稿日時: 2020-10-24 22:13:24
更新日時: 2020-10-24 22:13:24
クイックソート | 実行ファイルのパス

Comment