Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nameof expression in .net framework 4

Tags:

c#

c#-6.0

nameof

"nameof" expression is introduced in Visual Studio 2015 and c# 6

nameof (C# and Visual Basic Reference)

How can u use it or write a similar method in older versions like .net framework 4.

like image 447
MSL Avatar asked Jul 07 '15 07:07

MSL


People also ask

What is Nameof () C#?

C# NameOf operator is used to get name of a variable, class or method. It returns a simple string as a result. In error prone code, it is useful to capture a method name, in which error occurred. We can use it for logging, validating parameters, checking events etc.

What is the Nameof?

idiom. : used to indicate the name that is used for someone or something.

Is Nameof reflection?

Does it use Reflection? nameof is apparently as efficient as declaring a string variable. No reflection or whatsoever!

What is new in c6?

C# 6 has a new feature called string interpolation using which you can now directly write your arguments instead of referring them with placeholders inside a string . You can also do whatever you would have done previously with String. Format() function.


2 Answers

If you're talking about an equivalent for C# before C#6, this will get the job done (in a hacky way) for properties. It can probably be expanded upon to include fields, methods, etc.

public static class TestExtension {     public static String nameof<T, TT>(this T obj, Expression<Func<T, TT>> propertyAccessor)     {         if (propertyAccessor.Body.NodeType == ExpressionType.MemberAccess)         {             var memberExpression = propertyAccessor.Body as MemberExpression;             if (memberExpression == null)                 return null;             return memberExpression.Member.Name;         }         return null;     } } 

Just whipped this up quickly, so there's a lot to be improved, but you use it like this:

public class myClass {     public string myProp { get; set; } }  var a = new myClass(); var result = a.nameof(b => b.myProp); 

Result contains 'myProp'

Update:

More comprehensive (though still not that pretty)

public static class TestExtension {     public static String nameof<T, TT>(this Expression<Func<T, TT>> accessor)     {         return nameof(accessor.Body);     }      public static String nameof<T>(this Expression<Func<T>> accessor)     {         return nameof(accessor.Body);     }      public static String nameof<T, TT>(this T obj, Expression<Func<T, TT>> propertyAccessor)     {         return nameof(propertyAccessor.Body);     }      private static String nameof(Expression expression)     {         if (expression.NodeType == ExpressionType.MemberAccess)         {             var memberExpression = expression as MemberExpression;             if (memberExpression == null)                 return null;             return memberExpression.Member.Name;         }         return null;     } } 

Accessing static properties/fields:

TestExtension.nameof(() => myClass.MyOtherField) 

Accessing parameters within functions:

void func (int a) {     TestExtension.nameof(() => a); } 
like image 188
Rob Avatar answered Oct 18 '22 00:10

Rob


To my knowledge there are three options to not have to use a magic string

  1. nameof which requires Visual Studio 2015 (But can be compiled to other versions of the .net framework)

    nameof(this.Property) 
  2. use a method that takes an expression and returns the property name as found in this post "Get string name of property using reflection"

    var propertyName = GetPropertyName(       () => myObject.AProperty); // returns "AProperty" 
  3. CallerMemberNameAttribute - (Only available in .net framework 4.5, included because original post said older versions like .net framework 4.0 which I guess includes 4.5) The draw back of this method is it is only useful when you need to string representation of the current method you are operating in.

    public string IsChecked  {    set{        Console.WriteLine(GetCurrentMemberName()); // prints "IsChecked"    } }  string GetCurrentMemberName([CallerMemberName] string memberName = "") {      return memberName; } 
like image 37
JamesF Avatar answered Oct 17 '22 22:10

JamesF