11

I would like to get the name of a property, for example:

Dim _foo As String

Public Property Foo as String
Get
   Return _foo
End Get
Private Set(ByVal value as String)
   _foo = value
End Set

Sub Main()
  Console.Write(Foo.Name)'Outputs "Foo"
End Sub

Any ideas how?

1
  • You want the string "Name"? Commented Jul 27, 2010 at 8:01

4 Answers 4

24

Do you mean a property, or do you mean a field?

There are clever lambda techniques for getting the names of properties - here's a C# example:

String GetPropertyName<TValue>(Expression<Func<TValue>> propertyId)
{
   return ((MemberExpression)propertyId.Body).Member.Name;
}

Call it like this:

GetPropertyName(() => MyProperty)

and it will return "MyProperty"

Not sure if that's what you're after or not.

Sign up to request clarification or add additional context in comments.

5 Comments

My mistake my mind was somewhere else when I wrote the example. Changed it now
the call example should be: MyClass obj = new MyClass(); GetPropertyName(() => obj.MyProperty)
Do you know .NET 2.0 way of doing this?
In 2.0, you need to use reflection rather than Expressions.
is there any way to do this for a property on a other type then the current instance? without having a instance of it?
13

If you are using C# 6.0 (not released when this question was asked) you can use the following:

nameof(PropertyName)

This is evaluated at compile time and is converted to a string, the nice thing about using nameof() is that you don't have to manually change a string when you refactor. (nameof works on more than Properties, CallerMemberName is more restrictive)

If you are still stuck in pre c# 6.0 then you can use CallerMemberNameAttribute (it requires .net 4.5)

private static string Get([System.Runtime.CompilerServices.CallerMemberName] string name = "")
{
    if (string.IsNullOrEmpty(name))
        throw new ArgumentNullException("name");
    return name;
}

1 Comment

I don't why this was not well appreciated, but this method is really neat. Thanks
3
public static PropertyInfo GetPropInfo<T>(this T @object
    , Expression<Action<T>> propSelector)
{
    MemberExpression exp= propSelector.Body as MemberExpression;
    return exp.Member as PropertyInfo;
}

Then use it like this:

string str = ....
string propertyName = str.GetPropInfo(a => a.Length).Name;

Note that the above method is an extension and should be written in a static class and used by including the namespace

1 Comment

Or in the case of the OP, in VB it's an extension method that should be in a Module (not static class) and decorated with the Extension attribute rather than the this keyword.
0

via reflection. Use GetType() method of the type, then look into GetProperties() method and PropertyInfo class. (if you'd like to retrieve the string "propertyName" (for a field called propertyName - use xxx.GetType().GetFields()[0].Name if it's first field in the class.

1 Comment

This works only if you know the index of the property in the class, which is not really realiable as the class can change or may be partial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.