With .NET 4.5, the CLR team added:
CallerFilePathAttribute
CallerLineNumberAttribute
CallerMemberNameAttribute
Why wasn't one added for the type of the caller?
This is difficult for someone outside the design team to answer but I would say that there is not a strong use case for CallerTypeNameAttribute
.
The file and line attributes give you extended information for logging routines that would be otherwise impossible to obtain. The member name allows logging and simplifies the implementation of INotifyPropertyChanged
while also allowing safe name refactoring without the need to search strings.
The caller type can already be passed to a given method by using typeof(CurrentType).Name
so it probably does not merit an extra attribute. You could say that the caller member name could also already be obtained using MethodBase.GetCurrentMethod
but that probably always forces reflection and the typeof
is probably optimized away so you already get the benefit of safe refactoring and a lesser impact on performance.
The only downside of using typeof
instead of the possible attribute would be that the attribute approach would not be affected by obfuscation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With