How to Get the Propertyinfo of a Specific Property

How to get the PropertyInfo of a specific property?

You can use the new nameof() operator that is part of C# 6 and available in Visual Studio 2015. More info here.

For your example you would use:

PropertyInfo result = typeof(MyObject).GetProperty(nameof(MyObject.MyProperty));

The compiler will convert nameof(MyObject.MyProperty) to the string "MyProperty" but you gain the benefit of being able to refactor the property name without having to remember to change the string because Visual Studio, ReSharper, and the like know how to refactor nameof() values.

How do you get the Value of a property from PropertyInfo?

Unless the property is static, it is not enough to get a PropertyInfo object to get a value of a property. When you write "plain" C# and you need to get a value of some property, say, MyProperty, you write this:

var val = obj.MyProperty;

You supply two things - the property name (i.e. what to get) and the object (i.e. from where to get it).

PropertyInfo represents the "what". You need to specify "from where" separately. When you call

var val = info.GetValue(obj);

you pass the "from where" to the PropertyInfo, letting it extract the value of the property from the object for you.

Note: prior to .NET 4.5 you need to pass null as a second argument:

var val = info.GetValue(obj, null);

Get Property Value from PropertyInfo

Update to:

foreach (PropertyInfo propertyInfo in md.GetType().GetProperties())
{
new FormMetaData
{
FormFieldName = propertyInfo.Name,
MetadataLabel = propertyInfo.GetValue(md) // <--
}
}

PropertyInfo.GetValue() expects an instance of the object that
contains the property whose value you're trying to get. In your
foreach loop, that instance seems to be md.


Also note the distinction between property and field in C#. Properties are the members that have get and/or set:

class MyClass {
string MyProperty {get; set;} // This is a property
string MyField; // This is a field
}

And while reflecting, you need to access these members separately via the myObj.GetType().GetProperties() and myObj.GetType().GetFields() methods.

Get Property Info from an object without giving the property name as string

Something like this?

string s = GetPropertyName<User>( x=> x.Name );

public string GetPropertyName<T>(Expression<Func<T, object>> lambda)
{
var member = lambda.Body as MemberExpression;
var prop = member.Member as PropertyInfo;
return prop.Name;
}

or

public PropertyInfo GetPropertyInfo<T>(Expression<Func<T, object>> lambda)
{
var member = lambda.Body as MemberExpression;
return member.Member as PropertyInfo;
}

public class User
{
public string Name { set; get; }
}

How to get PropertyInfo of C# object

using System;
using System.Reflection;

class Example
{
public static void Main()
{
string test = "abcdefghijklmnopqrstuvwxyz";

// Get a PropertyInfo object representing the Chars property.
PropertyInfo pinfo = typeof(string).GetProperty("Chars");

// Show the first, seventh, and last letters
ShowIndividualCharacters(pinfo, test, 0, 6, test.Length - 1);

// Show the complete string.
Console.Write("The entire string: ");
for (int x = 0; x < test.Length; x++)
{
Console.Write(pinfo.GetValue(test, new Object[] {x}));
}
Console.WriteLine();
}

static void ShowIndividualCharacters(PropertyInfo pinfo,
object value,
params int[] indexes)
{
foreach (var index in indexes)
Console.WriteLine("Character in position {0,2}: '{1}'",
index, pinfo.GetValue(value, new object[] { index }));
Console.WriteLine();
}
}

c# reflection - getting list of properties from a PropertyInfo

Assuming I understand what you are trying to do:

PropertyInfo represents a property of a class without being aware of the instance of the class whose property is being inspected.

GetValue method, however, can provide you the value of the property for a given instance.

object value = somePropertyInfo.GetValue(someInstance);
// where someInstance is of the type which has someProperty's represented property.

If you want the properties of the Type of the property you are currently inspecting you can use PropertyInfo.PropertyType.GetProperties(); but this will only get you the properties of the Type of the property and not the concrete (maybe derived) Type that it contains.

C# Reflection - Get PropertyInfo without a string

See here. The idea is to use Expression Trees.

public static string GetPropertyName<T, TReturn>(Expression<Func<T, TReturn>> expression)
{
MemberExpression body = (MemberExpression)expression.Body;
return body.Member.Name;
}

And then use it like:

var name = GetPropertyName<MyClass, string>(c => c.FirstName);

A bit cleaner solution would be if one would not required to specify so much generic parameters. And it is possible via moving MyClass generic param to util class:

public static class TypeMember<T>
{
public static string GetPropertyName<TReturn>(Expression<Func<T, TReturn>> expression)
{
MemberExpression body = (MemberExpression)expression.Body;
return body.Member.Name;
}
}

Then usage will be cleaner:

var name = TypeMember<MyClass>.GetPropertyName(c => c.FirstName);

Is there any way to get the PropertyInfo from the getter of that property?

MethodBase.GetCurrentMethod() will return the MethodInfo object for get_YourPropertyName.

PropertyInfo property = GetType()
.GetProperty(MethodBase
.GetCurrentMethod()
.Name
.Substring("get_".Length)
);

Get property value from string using reflection

 public static object GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}

Of course, you will want to add validation and whatnot, but that is the gist of it.



Related Topics



Leave a reply



Submit