How to Get a Property Value Using Reflection

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.

How to get a property value using reflection

Something like this should work:

var value = (string)GetType().GetProperty("SomeProperty").GetValue(this, null);

How get property value with reflection in C#

Once you've retrieved the PropertyInfo, you fetch the value with PropertyInfo.GetValue, passing in "the thing you want to get the property from" (or null for a static property).

Here's an example:

using System;
using System.Reflection;

class Program
{
static void Main()
{
DateTime utcNow = DateTime.UtcNow;

PropertyInfo dateProperty = typeof(DateTime).GetProperty("Date");
PropertyInfo utcNowProperty = typeof(DateTime).GetProperty("UtcNow");

// For instance properties, pass in the instance you want to
// fetch the value from. (In this case, the DateTime will be boxed.)
DateTime date = (DateTime) dateProperty.GetValue(utcNow);
Console.WriteLine($"Date: {date}");

// For static properties, pass in null - there's no instance
// involved.
DateTime utcNowFromReflection = (DateTime) utcNowProperty.GetValue(null);
Console.WriteLine($"Now: {utcNowFromReflection}");
}
}

Get raw value of property attribute using Reflection

Since attribute constructor information are stored as metadata as well you can get the required information by calling the GetCustomAttributesData method. Have a look at this simple example:

class Program
{
[PasswordPropertyText]
public string Password1 { get; set; }

[PasswordPropertyText(true)]
public string Password2 { get; set; }

[PasswordPropertyText(false)]
public string Password3 { get; set; }

static void Main(string[] args)
{
var props = typeof(Program).GetProperties();
foreach(var prop in props)
{
var attributeData = prop.GetCustomAttributesData().First(x => x.AttributeType == typeof(PasswordPropertyTextAttribute));
Console.WriteLine($"{prop.Name}: {(attributeData.ConstructorArguments.Cast<CustomAttributeTypedArgument?>().FirstOrDefault()?.Value ?? true)}");
}
Console.ReadLine();
}
}

Output:

Password1: True
Password2: True
Password3: False

How to get a property value based on the name

return car.GetType().GetProperty(propertyName).GetValue(car, null);

Get property value of a complex type with reflection in c#

You're trying to get the value of a property as if it were a property of string - the first argument to GetValue needs to be the object you're fetching the property from (oldEntity in this case). So you can get the department like this:

object department = oldEntity.GetType().GetProperty("Department").GetValue(oldEntity, null);

To get the Name property (which I hope is a property and not a public field) you need to do the same kind of thing again:

object name = department.GetType().GetProperty("Name").GetValue(department, null);

However, you could do this all somewhat more simply using dynamic typing:

dynamic entity = oldEntity;
string name = entity.Department.Name;

Note that there's no compile-time checking for the Department or Name parts (or that the resulting type is string) but that's the same as for the reflection-based code anyway. The dynamic typing would just do the reflection for you at execution time.

How to get a property value using Reflection if the property name is defined as name

Ok, I figured it out... I guess uppercase counts when doing reflection...

I needed to change the code invocation to...

//code invocation
CollectionComparer comp = new CollectionComparer("Name", "ASC");
this.InnerList.Sort(comp);

As the property was actually called "Name" and not "name"

Using reflection to get values from properties from a list of a class

To Get/Set using reflection you need an instance. To loop through the items in the list try this:

PropertyInfo piTheList = MyObject.GetType().GetProperty("TheList"); //Gets the properties

IList oTheList = piTheList.GetValue(MyObject, null) as IList;

//Now that I have the list object I extract the inner class and get the value of the property I want

PropertyInfo piTheValue = piTheList.PropertyType.GetGenericArguments()[0].GetProperty("TheValue");

foreach (var listItem in oTheList)
{
object theValue = piTheValue.GetValue(listItem, null);
piTheValue.SetValue(listItem,"new",null); // <-- set to an appropriate value
}


Related Topics



Leave a reply



Submit