Get & Set Object property value in C#

In C#, you can use reflection to get or set the value of an object's property. The process may vary depending on whether the property is public or private, and whether it is static or not.

Get Property Value from String Using Reflection

using System.Reflection;

object obj = new MyClass();
PropertyInfo property = obj.GetType().GetProperty("MyProperty");
object propertyValue = property.GetValue(obj);

In this example, we create a new instance of the MyClass class and store it in the obj variable. We then use the GetType method to get the Type object for the MyClass class, and the GetProperty method to get the PropertyInfo object for the MyProperty property. Finally, we use the GetValue method of the PropertyInfo object to get the value of the property as an object.

Get Private Property Value from String Using Reflection

using System.Reflection;

object obj = new MyClass();
Type type = obj.GetType();
PropertyInfo property = type.GetProperty("MyPrivateProperty", BindingFlags.Instance | BindingFlags.NonPublic);
object propertyValue = property.GetValue(obj);

In this example, we create a new instance of the MyClass class and store it in the obj variable. We then use the GetType method to get the Type object for the MyClass class, and the GetProperty method with BindingFlags to get the PropertyInfo object for the private MyPrivateProperty property. Finally, we use the GetValue method of the PropertyInfo object to get the value of the property as an object.

Get Static Property Value from Static Class Using Reflection

using System.Reflection;

object propertyValue = typeof(MyStaticClass).GetProperty("MyStaticProperty").GetValue(null);

In this example, we use the typeof operator to get the Type object for the MyStaticClass class, and the GetProperty method to get the PropertyInfo object for the MyStaticProperty property. Finally, we use the GetValue method of the PropertyInfo object to get the value of the property as an object. Since the class is static, we pass null as the argument to the GetValue method.

Set Object Property Using Reflection

using System.Reflection;

object obj = new MyClass();
PropertyInfo property = obj.GetType().GetProperty("MyProperty");
property.SetValue(obj, "new value");

In this example, we create a new instance of the MyClass class and store it in the obj variable. We then use the GetType method to get the Type object for the MyClass class, and the GetProperty method to get the PropertyInfo object for the MyProperty property. Finally, we use the SetValue method of the PropertyInfo object to set the value of the property to "new value".

  1. C# get object property value

    Accessing the value of an object property:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    int propertyValue = obj.MyProperty;
    
  2. Accessing object properties in C#

    Basic access to object properties:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    int propertyValue = obj.MyProperty;
    
  3. Getting and setting properties in C#

    Getting and setting properties:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    obj.MyProperty = 42;
    int propertyValue = obj.MyProperty;
    
  4. C# get property value dynamically

    Getting property value dynamically using reflection:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    string propertyName = "MyProperty";
    int propertyValue = (int)obj.GetType().GetProperty(propertyName).GetValue(obj);
    
  5. Object property accessors in C#

    Property accessors allow encapsulation of property access:

    public class MyClass
    {
        private int myProperty;
    
        public int MyProperty
        {
            get { return myProperty; }
            set { myProperty = value; }
        }
    }
    
    // Usage
    MyClass obj = new MyClass();
    obj.MyProperty = 42;
    int propertyValue = obj.MyProperty;
    
  6. C# set object property value

    Setting the value of an object property:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    obj.MyProperty = 42;
    
  7. Accessing properties of an object in C#

    Accessing multiple properties of an object:

    public class MyClass
    {
        public int Property1 { get; set; }
        public string Property2 { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    int value1 = obj.Property1;
    string value2 = obj.Property2;
    
  8. C# get property value using reflection

    Getting property value using reflection:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    string propertyName = "MyProperty";
    int propertyValue = (int)obj.GetType().GetProperty(propertyName).GetValue(obj);
    
  9. Dynamically set object property in C#

    Dynamically setting the value of an object property using reflection:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    string propertyName = "MyProperty";
    obj.GetType().GetProperty(propertyName).SetValue(obj, 42);
    
  10. C# object property manipulation

    General manipulation of object properties:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    obj.MyProperty += 10;
    
  11. Get and set properties using PropertyInfo in C#

    Using PropertyInfo for getting and setting property values:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    PropertyInfo propertyInfo = obj.GetType().GetProperty("MyProperty");
    int propertyValue = (int)propertyInfo.GetValue(obj);
    propertyInfo.SetValue(obj, propertyValue + 10);
    
  12. C# set property value dynamically

    Setting property value dynamically using reflection:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    string propertyName = "MyProperty";
    obj.GetType().GetProperty(propertyName).SetValue(obj, 42);
    
  13. Accessing nested object properties in C#

    Accessing properties of nested objects:

    public class OuterClass
    {
        public InnerClass Inner { get; set; }
    }
    
    public class InnerClass
    {
        public int NestedProperty { get; set; }
    }
    
    // Usage
    OuterClass obj = new OuterClass { Inner = new InnerClass { NestedProperty = 42 } };
    int nestedPropertyValue = obj.Inner.NestedProperty;
    
  14. C# get property value by name

    Getting property value by name using reflection:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    string propertyName = "MyProperty";
    int propertyValue = (int)obj.GetType().GetProperty(propertyName).GetValue(obj);
    
  15. Setting private object properties in C#

    Setting the value of private properties using reflection:

    public class MyClass
    {
        private int privateProperty;
    
        public void SetPrivateProperty(int value)
        {
            privateProperty = value;
        }
    }
    
    // Usage
    MyClass obj = new MyClass();
    obj.SetPrivateProperty(42);
    
  16. C# set property value using reflection

    Setting property value using reflection:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    string propertyName = "MyProperty";
    obj.GetType().GetProperty(propertyName).SetValue(obj, 42);
    
  17. Object property access by index in C#

    Accessing properties using an index or key:

    public class MyDictionaryClass
    {
        public int this[string propertyName]
        {
            get { /* Implement getter */ }
            set { /* Implement setter */ }
        }
    }
    
    // Usage
    MyDictionaryClass obj = new MyDictionaryClass();
    obj["MyProperty"] = 42;
    int propertyValue = obj["MyProperty"];
    
  18. C# reflection get and set property

    Getting and setting property values using reflection:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    string propertyName = "MyProperty";
    int propertyValue = (int)obj.GetType().GetProperty(propertyName).GetValue(obj);
    obj.GetType().GetProperty(propertyName).SetValue(obj, propertyValue + 10);
    
  19. Dynamic object property access in C#

    Dynamic property access using the dynamic keyword:

    dynamic obj = new ExpandoObject();
    obj.MyProperty = 42;
    int propertyValue = obj.MyProperty;
    
  20. C# get and set property using lambda expressions

    Using lambda expressions for property access:

    public class MyClass
    {
        public int MyProperty { get; set; }
    }
    
    // Usage
    MyClass obj = new MyClass();
    Func<MyClass, int> getProperty = o => o.MyProperty;
    Action<MyClass, int> setProperty = (o, value) => o.MyProperty = value;
    
    int propertyValue = getProperty(obj);
    setProperty(obj, propertyValue + 10);