Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically access class and its property in C#

Tags:

c#

I need to access something like strClassname.strPropertyName I will have different values for strClassname and strProperty name in program execution.

Please direct me in right way.

like image 353
Rahul Vasantrao Kamble Avatar asked Dec 21 '12 06:12

Rahul Vasantrao Kamble


People also ask

What is dynamic property in C#?

Dynamic objects expose members such as properties and methods at run time, instead of at compile time. This enables you to create objects to work with structures that do not match a static type or format.

How to get class property value dynamically in C#?

In C#, you can access dynamic properties by obtaining a PropertyObject reference from the specific object reference using the AsPropertyObject method on the object. You can then use the PropertyObject interface to access custom properties of the object using a lookup string to specify the specific custom property.


2 Answers

Sounds to me like you are trying to get (or set) the value of a property on an object at runtime. So here's the most basic way to do this:

public static object GetPropertyValue(object instance, string strPropertyName)
{
    Type type = instance.GetType();
    System.Reflection.PropertyInfo propertyInfo = type.GetProperty(strPropertyName);
    return propertyInfo.GetValue(instance, null);
}

... and to set a value:

public static void SetPropertyValue(object instance, string strPropertyName, object newValue)
{
    Type type = instance.GetType();
    System.Reflection.PropertyInfo propertyInfo = type.GetProperty(strPropertyName);
    propertyInfo.SetValue(instance, newValue, null);
}

If you're attempting to get the names of properties of a class, here's a function for that:

public static IEnumerable<string> GetPropertyNames(string className)
{
    Type type = Type.GetType(className);
    return type.GetProperties().Select(p => p.Name);
}

Say that you have 100 objects, and you want to get the value of the Name property on each of them, here's a function that will do that:

public static IEnumerable<String> GetNames(IEnumerable<Object> objects, string nameProperty = "Name")
{
    foreach (var instance in objects)
    {
        var type = instance.GetType();
        var property = type.GetProperty(nameProperty);
        yield return property.GetValue(instance, null) as string;
    }
}
like image 69
David Schwartz Avatar answered Oct 03 '22 05:10

David Schwartz


You can use reflection:

To get names of properties for a specific type use method Type.GetProper­ties. Method returns array of PropertyInfo objects and the property names are available through PropertyInfo.Name property. If you want to get only subset of all properties (e.g. only public static ones) use BindingFlags when calling GetProperties method. You have to specify at least two flags, one from Public/NonPublic and one of Instance/Static flags. If you use GetProperties without a BindingFlags parameter, default flags are Public + NonPublic + Instance.

Following example shows how to get public static properties.

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);
}

[Source]

like image 31
rekire Avatar answered Oct 03 '22 07:10

rekire