C# 5.0-.Net新特性

调用者信息特性

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
    { }
}

 

上一篇:asm数据文件迁移(os–>asm)


下一篇:RedisJson和RedisSearch探究(三)