1. 程式人生 > >C# 5.0-.Net新特性

C# 5.0-.Net新特性

param size pil pat pan ber ram 定義 4.5

調用者信息特性

CallerMemberNameAttribute | CallerFilePathAttribute | CallerLineNumberAttribute

.NET Framework 4.5 中新增,用於請求編譯器在編譯過程中進行代碼的轉換 。

使用方式:直接調用即可

public static void TraceMessage(string message, string errCode, 
                [CallerMemberNameAttribute] string memberName = "",
                [CallerFilePathAttribute] string filePath = "",
                [CallerLineNumberAttribute] int lineNumber = 0)

若要在 .NET Framework 4.0 中使用,需自定義特性

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
    public class CallerMemberNameAttribute : Attribute
    { }

    [AttributeUsage(AttributeTargets.Parameter, Inherited = false )]
    public class CallerFilePathAttribute : Attribute
    { }

    [AttributeUsage(AttributeTargets.Parameter, Inherited = false )]
    public class CallerLineNumberAttribute : Attribute
    { }
}

C# 5.0-.Net新特性