Caller Info In C# 5.0

Here we are with the second new feature in C# 5.0 “Caller Info“.

Lets start with C# evolution chart, then with an example shows How to use Caller Information.

C# Evolution Chart

Caller Information Example

Suppose we have DoSomething method that will do some important thing.

And we need to log some information about Who call it and When.
We can do the following…

    class Program
    {
        static void Main(string[] args)
        {
            InsertLog("Main");
            DoSomething();

            MethodA();

            MethodB();

            Console.ReadLine();
        }

        static void MethodA()
        {
            InsertLog("MethodA");
            DoSomething();
        }

        static void MethodB()
        {
            InsertLog("MethodB");
            DoSomething();
        }

        static void DoSomething()
        {
            // Some important thing will be here.
        }

        static void InsertLog(string methodName)
        {
            Console.WriteLine("{0} called DoSomething at {1}", methodName, DateTime.Now);
            Console.WriteLine("-----------------------------------------------------");
        }
    }

Each time before calling DoSomething we call InsertLog and pass the caller method name for logging.

Now note how we can do the same thing but using the Caller Info. It’s really simple 🙂

    using System.Runtime.CompilerServices;

    class Program
    {
        static void Main(string[] args)
        {
            DoSomething();

            MethodA();

            MethodB();

            Console.ReadLine();
        }

        static void MethodA()
        {
            DoSomething();
        }

        static void MethodB()
        {
            DoSomething();
        }

        static void DoSomething([CallerMemberName] string memberName = "",
                            [CallerFilePath] string sourceFilePath = "",
                            [CallerLineNumber] int sourceLineNumber = 0)
        {
            InsertLog(memberName, sourceFilePath, sourceLineNumber);
            // Some important thing will be here.
        }

        static void InsertLog(string methodName, string sourceFilePath, int sourceLineNumber)
        {
            Console.WriteLine("{0} called methodB at {1}", methodName, DateTime.Now);
            Console.WriteLine("Source file path: {0}", sourceFilePath);
            Console.WriteLine("Source line number: {0}", sourceLineNumber);
            Console.WriteLine("-----------------------------------------------------");
        }
    }

Instead of passing the caller method name we can use CallerMemberName.
Note that InsertLog now exists in only one place DoSomething and now we can know who call DoSomething, the source file path, and the source line number. All without need to do extra work. It’s really amazing and helpful 🙂

You can find here more information about Caller Info and how to use it.

Thank you,

Keep It Simple & Straightforward 🙂

~:H}{H:~

Advertisements

2 thoughts on “Caller Info In C# 5.0

  1. Very good article Hossam, Thanks 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s