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
        {
    
        }
    }
  • 相关阅读:
    第十周总结
    冲刺(四)
    冲刺(三)
    冲刺(二)
    冲刺(一)
    生成热词
    c#简单日志类
    WPF 后台代码 实现DynamicResource 绑定赋值
    WPF ListboxItem 双击事件 Command绑定
    mysql的命令行安装,忘记密码,密码重置问题
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/11311977.html
Copyright © 2011-2022 走看看