C# Object Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
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.
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.
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.
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.
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"
.
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;
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;
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;
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);
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;
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;
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;
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);
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);
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;
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);
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);
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;
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);
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);
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);
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"];
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);
Dynamic object property access in C#
Dynamic property access using the dynamic
keyword:
dynamic obj = new ExpandoObject(); obj.MyProperty = 42; int propertyValue = obj.MyProperty;
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);