0

I have the following model:

Public Class MyModel

    Public Property MyModelId As Integer
    Public Property Description As String
    Public Property AnotherProperty As String

End Class

Is there a method to get a property name of the Model as a string representation like the following code?

Dim propertyName as String = GetPropertyNameAsStringMethod(MyModel.Description)

So the propertyName variable has "Description" as value.

Gaz Winter
  • 2,924
  • 2
  • 25
  • 47
Max
  • 4,965
  • 17
  • 49
  • 64
  • 1
    there are no such method that do as you want. you get the properties name using reflection. ` string prop = "name"; PropertyInfo pi = myObject.GetType().GetProperty(prop);` – Niranjan Singh Jan 04 '13 at 14:46
  • Duplicate : http://stackoverflow.com/questions/3341666/net-get-property-name – jtiger Jan 04 '13 at 14:49

5 Answers5

2

Check the Darin Dimitrov' answer on this SO thread - Reflection - get property name.

class Foo
{
    public string Bar { get; set; }
}

class Program
{
    static void Main()
    {
        var result = Get<Foo, string>(x => x.Bar);
        Console.WriteLine(result);
    }

    static string Get<T, TResult>(Expression<Func<T, TResult>> expression)
    {
        var me = expression.Body as MemberExpression;
        if (me != null)
        {
            return me.Member.Name;
        }
        return null;
    }
}

Hope this help..

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
1

Here is a helper extension method you can use for any property:

public static class ReflectionExtensions
{
    public static string PropertyName<T>(this T owner, 
        Expression<Func<T, object>> expression) where T : class
    {
        if (owner == null) throw new ArgumentNullException("owner");
        var memberExpression = (MemberExpression)expression.Body;
        return memberExpression.Member.Name;
    }

}

However, this will only work on instances of a class. You can write a similar extension method that will operate directly on the type instead.

danludwig
  • 46,965
  • 25
  • 159
  • 237
1

You need to do it using reflection.

There are already loads of posts on stack overflow like this:

How to get current property name via reflection?

Reflection - get property name

Get string name of property using reflection

Reflection - get property name

I believe that the answer will be along the lines of:

 string prop = "name"; 
 PropertyInfo pi = myObject.GetType().GetProperty(prop);
Community
  • 1
  • 1
Gaz Winter
  • 2,924
  • 2
  • 25
  • 47
0

Create an extension method and then use it where needed.

Private Shared Function GetPropertyName(Of T)(exp As Expression(Of Func(Of T))) As String
    Return (DirectCast(exp.Body, MemberExpression).Member).Name
End Function

have a look at this post as well.

Community
  • 1
  • 1
Syneryx
  • 1,278
  • 11
  • 18
0

I have solved this issue editing a bit @NiranjanKala's source example,

converting the code in vb.Net like this

<System.Runtime.CompilerServices.Extension()> _
Public Function GetPropertyName(Of T, TResult)(expression As Expression(Of Func(Of T, TResult))) As String
    Dim [me] = TryCast(expression.Body, MemberExpression)
    If [me] IsNot Nothing Then
        Return [me].Member.Name
    End If
    Return Nothing
End Function

Then I am able to call the extension like this

Dim propertyName as String = GetPropertyName(Of MyModel, String)(Function(x) x.Description)

Then propertyName variable has "Description" as string value.

Max
  • 4,965
  • 17
  • 49
  • 64