zoukankan      html  css  js  c++  java
  • NET 4.5 中新增的特性调用者信息特性CallerMemberNameAttribute/CallerFilePathAttribute/CallerLineNumberAttribute

    标题中所说的三个特性 CallerMemberNameAttribute / CallerFilePathAttribute / CallerLineNumberAttribute 我们统称为调用者信息特性,正常情况下在 .NET Framework 4.0 中是无法使用的。

      static void Main( string[] args )
        {
            var productInfo = new ProductInfo();
    
            productInfo.Name = "lumia";
    
            productInfo.PropertyChanged();
    
            Console.ReadKey( true );
        }
    }
    
    public class ProductInfo
    {
        private string _name;
    
        public string Name
        {
            get { return this._name; }
            set
            {
                this._name = value;
                this.PropertyChanged();
            }
        }
    
        public void PropertyChanged([CallerMemberName]string name = "", [CallerLineNumber]int line = 0, [CallerFilePath]string file = "")
        {
            Console.WriteLine("------------------------------------------------");
            Console.WriteLine($"Name : {name}, 
    Line : {line}, 
    Path : {file}");
        }
    }

    在 .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
        {
    
        }
    }
  • 相关阅读:
    count(*) 和 count(1)和count(列名)区别
    网页横向滚动条
    发送公众号模板消息
    tp中S与session()
    php 判断sql执行时间
    thinkphp联查
    php 获取当前时间
    微信分享
    测试用手机奇怪问题
    翻译|多少植物才能净化室内空气?
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/11311977.html
Copyright © 2011-2022 走看看