0

I wrote an extension method for strings which I'm using for logging the name, the value and the "source" of the string which calls the method. Source meens from which class or methods comes the string. For example Program.Main() or ClasXY.ToString() ... It looks like this

public static void LogMe(this string str, string memberName, string methodName)
{
    WriteToLog(DateTime.Now, memberName, methodName, str);
}

Is it possible to get the name and the source of the string directly from the string object in my extension method? Something like this

public static void LogMe(this string str)
{
    string memberName = str.myName;
    string methodName = str. mySource;
    WriteToLog(DateTime.Now, memberName, methodName, str);
}
  • No, string does not contain a definition for `myName` - You would have to write another extension method, and then store that somewhere – Sayse Sep 03 '14 at 14:09
  • If it is possible, you will need to look at using reflection. By the way, take a look at this SO answer (http://stackoverflow.com/a/3095731/975724). It explains how to obtain the method/class name where where your method is invoked. – Nicholas Miller Sep 03 '14 at 14:15
  • Also, you may want to take a look at this SO question which covers how to obtain the variable name as it is typed within your code. (http://stackoverflow.com/q/9801624/975724) – Nicholas Miller Sep 03 '14 at 14:19

0 Answers0