Answers for "get property by name c#"

C#
2

c# get object property value by name

return car.GetType().GetProperty(propertyName).GetValue(car, null);
Posted by: Guest on April-24-2020
0

c# get property using string

public static object GetPropValue(object src, string propName)
 {
     return src.GetType().GetProperty(propName).GetValue(src, null);
 }
Posted by: Guest on July-07-2020
0

find class property with string C#

//Add this to class
public class MyClass 
{
    public object this[string property]
    {
        get
        {
            return typeof(security).GetProperty(property).GetValue(this, null);
        }
        set
        {
            typeof(security).GetProperty(property).SetValue(this, value, null);
        }
    }
}
Posted by: Guest on September-23-2020
-2

C# get object property name

using System.Reflection;  // reflection namespace

// get all public static properties of MyClass type
PropertyInfo[] propertyInfos;
propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public |
                                              BindingFlags.Static);
// sort properties by name
Array.Sort(propertyInfos,
        delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)
        { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });

// write property names
foreach (PropertyInfo propertyInfo in propertyInfos)
{
  Console.WriteLine(propertyInfo.Name);
}
Posted by: Guest on May-07-2020

Code answers related to "get property by name c#"

C# Answers by Framework

Browse Popular Code Answers by Language