標題中所說的三個特性 CallerMemberNameAttribute / CallerFilePathAttribute / CallerLineNumberAttribute 咱們統稱爲調用者信息特性,正常狀況下在 .NET Framework 4.0 中是沒法使用的。this
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}, \nLine : {line}, \nPath : {file}"); } }
在 .NET Framework 4.0 中使用須要本身定義這三個特性spa
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 { } }