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 the property name of an object. The process is slightly different in C# 6, so we will cover both options below.
using System.Reflection; object obj = new MyClass(); PropertyInfo property = obj.GetType().GetProperty("MyProperty"); string propertyName = property.Name;
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 Name
property of the PropertyInfo
object to get the name of the property as a string.
object obj = new MyClass(); string propertyName = nameof(obj.MyProperty);
In this example, we create a new instance of the MyClass
class and store it in the obj
variable. We then use the nameof
operator to get the name of the MyProperty
property as a string. This is a shorthand way of getting the property name without using reflection. Note that this syntax is only available in C# 6 and later.
C# get object property name
Accessing the name of an object property:
public class MyClass { public int MyProperty { get; set; } } // Usage MyClass obj = new MyClass(); string propertyName = nameof(obj.MyProperty); // "MyProperty"
Dynamic property name access in C#
Accessing property name dynamically:
dynamic obj = new ExpandoObject(); obj.MyProperty = 42; string propertyName = "MyProperty"; int propertyValue = obj.GetType().GetProperty(propertyName).GetValue(obj);
C# get property name using reflection
Getting property name using reflection:
public class MyClass { public int MyProperty { get; set; } } // Usage MyClass obj = new MyClass(); string propertyName = obj.GetType().GetProperties()[0].Name; // "MyProperty"
Accessing object property names in C#
Accessing all property names of an object:
public class MyClass { public int MyProperty1 { get; set; } public string MyProperty2 { get; set; } } // Usage MyClass obj = new MyClass(); string[] propertyNames = obj.GetType().GetProperties().Select(p => p.Name).ToArray();
C# set object property name dynamically
Setting the value of an object property 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);
Object property name accessors in C#
Property accessors allow encapsulation of property names:
public class MyClass { private string myProperty; public string MyProperty { get { return myProperty; } set { myProperty = value; } } } // Usage MyClass obj = new MyClass(); obj.MyProperty = "Hello";
C# set property value by property name
Setting property value by property name 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);
Dynamically getting and setting property names in C#
Dynamically getting and setting property names 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);
C# get property name from lambda expression
Getting property name from a lambda expression:
public class MyClass { public int MyProperty { get; set; } } // Usage MyClass obj = new MyClass(); string propertyName = GetPropertyName(() => obj.MyProperty); static string GetPropertyName<T>(Expression<Func<T>> propertyExpression) { var memberExpression = propertyExpression.Body as MemberExpression; return memberExpression?.Member.Name; }
Accessing property names of an object in C#
Accessing property names using reflection:
public class MyClass { public int MyProperty1 { get; set; } public string MyProperty2 { get; set; } } // Usage MyClass obj = new MyClass(); var propertyNames = obj.GetType().GetProperties().Select(p => p.Name).ToList();
C# set property value using dynamic property name
Setting property value dynamically using a dynamic object:
dynamic obj = new ExpandoObject(); string propertyName = "MyProperty"; obj.MyProperty = 42;
Dynamic object property access in C#
Dynamic property access using dynamic
keyword:
dynamic obj = new ExpandoObject(); obj.MyProperty = 42; string propertyName = "MyProperty"; int propertyValue = obj.MyProperty;
C# reflection get and set property name
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);
Getting all property names of an object in C#
Getting all property names of an object using reflection:
public class MyClass { public int MyProperty1 { get; set; } public string MyProperty2 { get; set; } } // Usage MyClass obj = new MyClass(); var propertyNames = obj.GetType().GetProperties().Select(p => p.Name).ToList();
C# set property value by string name
Setting property value by string name 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 property names in C#
Accessing property names 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 } }; var nestedPropertyNames = obj.Inner.GetType().GetProperties().Select(p => p.Name).ToList();
C# get property name from expression
Getting property name from a lambda expression:
public class MyClass { public int MyProperty { get; set; } } // Usage MyClass obj = new MyClass(); string propertyName = GetPropertyName(() => obj.MyProperty); static string GetPropertyName<T>(Expression<Func<T>> propertyExpression) { var memberExpression = propertyExpression.Body as MemberExpression; return memberExpression?.Member.Name; }
Dynamic object property access using dictionaries in C#
Using dictionaries for dynamic object property access:
IDictionary<string, object> obj = new ExpandoObject(); obj["MyProperty"] = 42;
C# reflection get property names dynamically
Getting property names using reflection dynamically:
public class MyClass { public int MyProperty1 { get; set; } public string MyProperty2 { get; set; } } // Usage MyClass obj = new MyClass(); var propertyNames = obj.GetType().GetProperties().Select(p => p.Name).ToList();
Accessing private object property names in C#
Accessing private property names using reflection:
public class MyClass { private int privateProperty; public void SetPrivateProperty(int value) { privateProperty = value; } } // Usage MyClass obj = new MyClass(); var privatePropertyNames = obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .Select(f => f.Name) .ToList();