MethodBase.GetCurrentMethod (). Name vs. [CallerMemberName]

What are the differences and impact on the code?

What about performance and limitations, which is better?

New Attributes:
- [CallerFilePathAttribute]
- [CallerMemberName]
- [CallerLineNumber]

Today they are also available in .NET 4 (Easy to develop and seems magical). Are their values ​​compiled or resolved at runtime?

+4
source share
1 answer

Firstly, MethodBase.GetCurrentMethod() returns the current method , while you can use [CallerMemberName] , etc. to pass some information about the calling method to the current method.

The former is evaluated at runtime using reflection, which is relatively slow, while the latter is processed at compile time and is essentially inefficient. I really tested this in tests. Reflection will cost you about 20 microseconds each time, which can be very significant if called frequently, while the [Caller...] attributes are not measurable.

+7
source

Source: https://habr.com/ru/post/1491229/


All Articles