Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get string name of property using reflection

There is a whole wealth of reflection examples out there that allow you to get either:

    1. All properties in a class

    2. A single property, provided you know the string name

Is there a way (using reflection, TypeDescriptor, or otherwise) to get the string name of a property in a class at runtime, provided all I have is an instance of the class and property?

EDIT I know that I can easily get all the properties in a class using reflection and then get the name of each property. What I'm asking for is a function to give me name of a property, provided I pass it the instance of the property. In other words, how do I find the property I want from the PropertyInfo[] array returned to me from the class.GetType().GetProperty(myProperty) so that I can get the PropertyInfo.Name from it?

like image 845
Joel B Avatar asked Sep 07 '10 19:09

Joel B


People also ask

How do I find property values?

You can determine home value by using an online valuation tool, hiring an appraiser, using a real estate agent, or checking comparable homes in your area. Using an online valuation tool or pulling comps in your neighborhood is easy and quick, but you'll receive more accurate results using a REALTOR® or appraiser.

How do you get the properties name of an object in typescript?

To dynamically access an object's property: Use keyof typeof obj as the type of the dynamic key, e.g. type ObjectKey = keyof typeof obj; . Use bracket notation to access the object's property, e.g. obj[myVar] .


2 Answers

If you already have a PropertyInfo, then @dtb's answer is the right one. If, however, you're wanting to find out which property's code you're currently in, you'll have to traverse the current call stack to find out which method you're currently executing and derive the property name from there.

var stackTrace = new StackTrace(); var frames = stackTrace.GetFrames(); var thisFrame = frames[0]; var method = thisFrame.GetMethod(); var methodName = method.Name; // Should be get_* or set_* var propertyName = method.Name.Substring(4); 

Edit:

After your clarification, I'm wondering if what you're wanting to do is get the name of a property from a property expression. If so, you might want to write a method like this:

public static string GetPropertyName<T>(Expression<Func<T>> propertyExpression) {     return (propertyExpression.Body as MemberExpression).Member.Name; } 

To use it, you'd write something like this:

var propertyName = GetPropertyName(     () => myObject.AProperty); // returns "AProperty" 
like image 90
Jacob Avatar answered Sep 21 '22 19:09

Jacob


With C# 6.0 (Visual Studio 2015), you can now use the nameof operator, like this:

var obj = new MyObject(); string propertyName = nameof(obj.Property); string methodName = nameof(obj.Method); string directPropertyName = nameof(MyObject.Property); string directMethodName = nameof(MyObject.Method); 
like image 39
saluce Avatar answered Sep 17 '22 19:09

saluce