28

"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.

leppie
  • 115,091
  • 17
  • 196
  • 297
MSL
  • 990
  • 1
  • 12
  • 28
  • There is a nasty way to use Expression. – leppie Jul 07 '15 at 07:13
  • 6
    The .NET Framework version isn't relevant, this is a C# 6 feature. If you're using VS 2015, you can use this and target any version of the framework. – Charles Mager Jul 07 '15 at 07:23
  • 4
    For anyone who is confused regarding the C#/.NET version mismatch, please refer to http://stackoverflow.com/questions/28921701/does-c-sharp-6-0-work-for-net-4-0 - tl;dr Roslyn converts C# 6 language features that are syntactic sugar, into code compatible with older .NET versions (so for example, `nameof(parameter)` becomes `"parameter"`). Note however, that as VS2015 is the only IDE to currently support Roslyn, this won't work in VS2013 and older. – Ian Kemp Sep 29 '15 at 07:50
  • The C#6 "nameof" keyword doesn't seem capable of returning a member name for any arbitrary type. For this, a custom NameOf method taking a parameter of type Expression> still seems necessary. – bugged87 Oct 15 '15 at 16:27

5 Answers5

30

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);
}
Rob
  • 26,989
  • 16
  • 82
  • 98
3

nameOf - Gets resolved at Compiletime - if you decompile, you will see that the compiler just translated the classname (without the Namespace(!)) into a constant string instead. So be aware!

If you want to get a classe's name use typeof() or GetType() to get the specific (maybe derived) type at Runtime and read the value of
the .Name-Property in .net < C#6.

Read more at MSDN

Cadburry
  • 1,844
  • 10
  • 21
  • 3
    `typeof(T).Name` will return the name of the type, not the name of the variable. – Charles W Jul 29 '16 at 16:22
  • 3
    I know this is old, but the point of `nameof` is to get a field or property name, not a type name, which was the original question asked. –  Apr 29 '18 at 19:30
3

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;
    }
    
Community
  • 1
  • 1
JamesF
  • 402
  • 4
  • 9
1

The nameof operator returns a string representation of the variable you have passed it so nameof(var1) will return "var1", its useful in avoiding code where we have to specificy variable names as strings like in argument exceptions.

In previous versions you could achieve a similar effect using reflection, or expression trees.

BhavO
  • 2,406
  • 1
  • 11
  • 13
0

@Rob, thank you so much, but the static member access could be for the non-static class members too.

public static class TestExtension {
    public static String nameof<T>(this Expression<Func<T, object>> accessor)
    {
        return nameof(accessor.Body);
    }
}
class MyClass {
    public Guid MyOtherField;
}
TestExtension.nameof<MyClass>(o=>o.MyOtherField);

and the good news is the Class won't be instantiated. It is useful to access domain layer DTOs.