Get Property Value from C# Dynamic Object by String (Reflection)

Get value of c# dynamic property via string

Once you have your PropertyInfo (from GetProperty), you need to call GetValue and pass in the instance that you want to get the value from. In your case:

d.GetType().GetProperty("value2").GetValue(d, null);

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.

Get properties of a Dynamic Type

You can use reflection to get the properties out and convert it to a dictionary:

dynamic v = new { A = "a" };

Dictionary<string, object> values = ((object)v)
.GetType()
.GetProperties()
.ToDictionary(p => p.Name, p => p.GetValue(v));

Dynamic Property to list in c# from reflection

You can convert with a function like this.

If the reflected object is not enumerable, it can't be converted.

public static List<T> ConvertMysteriousObjectToList<T>(object input)
{
var enumerable = input as IEnumerable;
if (enumerable == null) throw new InvalidOperationException("The object is not convertible to a list.");
return enumerable.Cast<T>().ToList();
}

If you're trying to convert some flavor of DbSet (e.g. a DbSet<Foo>) then you'd call it like this:

var o = ClassObject.GetType().GetProperty(classname);
var list = ConvertMysteriousObjectToList<Foo>(o);

If you don't know the type:

var list = ConvertMysteriousObjectToList<object>(o);

or

var list = ConvertMysteriousObjectToList<dynamic>(o);

How to dynamically get the instance of an object's property using reflection

You mistakenly believed you needed an instance of the property you were trying to set, but actually you need an instance of the object upon which you want to set the property. A property has no life outside of an object to which it belongs.

property.SetValue(this, val, null);

Is most likely what you were looking for.

Get the value from object of a class by property name dynamically in c#

You can use reflection to get the value:

public int GetValue(string field)
{
MyClass obj = new MyClass();
obj = FromDb(); //get value from db
var property = obj.GetType().GetProperty(field);
return (int)property.GetValue(obj);
}

Iterate over dynamic object fields and get field data type

use reflection. it will work just as well if you use object type instead of dynamic, or allow type inference with var:

var dynamicObject = new
{
Name = "Bla",
Age = 2,
Surname = "Bla Bla"
};
int index = 2;
System.Reflection.PropertyInfo[] allProps = dynamicObject.GetType().GetProperties();
System.Reflection.PropertyInfo prop = allProps[index];
Type type = prop.PropertyType;

How to get property value of a dynamic type where property name is in a variable in C#

Does it have to use Reflection? You know that JObject.Parse will return JObject, so you can see what are the public methods/ properties. You can see that it does not expose public property of JSON, hence you cannot get the value.

There are several ways to get the value without Reflection:

string jsonKey = "key2";
string json = "{\"key1\":\"value1\", \"key2\": \"value2\"}";
dynamic d = JObject.Parse(json);
string jsonValue1 = d.Value<string>(jsonKey); // one way
string jsonValue2 = (string)d[jsonKey]; // another way


Related Topics



Leave a reply



Submit