Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no CallerTypeNameAttribute in .NET 4.5?

Tags:

.net

clr

With .NET 4.5, the CLR team added:

  • CallerFilePathAttribute
  • CallerLineNumberAttribute
  • CallerMemberNameAttribute

Why wasn't one added for the type of the caller?

like image 489
Daniel A. White Avatar asked Aug 29 '12 20:08

Daniel A. White


1 Answers

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.

like image 100
João Angelo Avatar answered Oct 11 '22 21:10

João Angelo