Three Caller Info Attributes are
- [CallerMemberName] – It applies the caller’s member name
- [CallerFilePath] - It applies the path to the caller’s source code file
- [CallerLineNumber]- It applies the line number in the caller’s source code file
The below piece of code demonstrates all three attributes
1: using System;
2: using System.Runtime.CompilerServices;
3: class Program
4: {
5: static void Main()
6: {
7:Â Â Demo();
8: }
9: static void Demo (
10: [CallerMemberName] string memberName = null,
11: [CallerFilePath] string filePath = null,
12: [CallerLineNumber] int lineNumber = 0)
13: {
14:Â Â Â Console.WriteLine (memberName);
15:Â Â Â Console.WriteLine (filePath);
16:Â Â Â Console.WriteLine (lineNumber);
17: }
18: }
The output of the code as below
Main
C:\Projects\Program.cs (Assuming your project is located in this path)
16
Caller Info attributes are useful for writing logging functions and for implementing change notification patterns. The following method can be called from property’s set accessor
1: void RaisePropertyChanged( [CallMemberName] string propertyName = null)
2: {
3:
4: }
More can be read here
Share this post : |