Is the CallerMemberName attribute in 4.5 “able to be faked”?

前端 未结 1 1721
再見小時候
再見小時候 2020-12-01 09:32

So .NET 4.5 introduces the CallerMemberNameAttribute, which seems like a godsend to anyone working with WPF and implementing INotifyPropertyChanged

相关标签:
1条回答
  • 2020-12-01 10:03

    Yes, you can, exactly as you could use LINQ and .NET 2, as you said. I use the following in a .NET 4.0 project with the VS2012 compiler with success:

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

    Be very careful that everyone on the project is using VS2012, otherwise they'll get silent bugs because CallerMemberNameAttribute didn't do anything and null was passed as the parameter default.

    Edit 2013-06-28: Consider installing the Microsoft.Bcl NuGet package that provides CallerMemberNameAttribute (and some other classes from .NET 4.5) for .NET 4 and Silverlight rather than doing it manually.

    0 讨论(0)
提交回复
热议问题