Properties
The ByteCobra.Reflection.PropertyExtensions class provides extension methods for working with properties via reflection, with thread safety. This allows you to get and set property values on object instances and types (including static properties) with the guarantee that the access is thread-safe, avoiding potential race conditions.
Methods
GetProperty<T>
Gets the value of a property from an object instance.
Parameters
classInstance
: The object instance from which to retrieve the property value.name
: The name of the property to retrieve.flags
: Optional. The binding flags used in the property lookup. Default isBindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
.timeout
: Optional. The maximum time to wait for the property access lock. Default is no timeout.
Returns: The value of the property if found and accessible; otherwise, the default value of the type T
.
Example
public class TestClass
{
public string InstanceProperty { get; set; }
public static string StaticProperty { get; set; }
}
var testInstance = new TestClass { InstanceProperty = "test value" };
var value = testInstance.GetProperty<string>("InstanceProperty");
// value will be "test value"
SetProperty<T>
Sets the value of a property on an object instance.
Parameters
classInstance
: The object instance on which to set the property value.name
: The name of the property to set.value
: The value to set for the property.flags
: Optional. The binding flags used in the property lookup. Default isBindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
.timeout
: Optional. The maximum time to wait for the property access lock. Default is no timeout.
Example
var testInstance = new TestClass();
testInstance.SetProperty("InstanceProperty", "new value");
// Now the 'InstanceProperty' will be set to "new value"
GetStaticProperty<T>
Gets the value of a static property from a type.
Parameters
type
: The type containing the static property.name
: The name of the static property to retrieve.flags
: Optional. The binding flags used in the property lookup. Default isBindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic
.timeout
: Optional. The maximum time to wait for the property access lock. Default is no timeout.
Returns: The value of the static property if found and accessible; otherwise, the default value of the type T.
Example
TestClass.StaticProperty = "static test value";
var value = typeof(TestClass).GetStaticProperty<string>("StaticProperty");
// value will be "static test value"
SetStaticProperty<T>
Sets the value of a static property on a type.
Parameters
type
: The type containing the static property.name
: The name of the static property to set.value
: The value to set for the static property.flags
: Optional. The binding flags used in the property lookup. Default isBindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic
.timeout
: Optional. The maximum time to wait for the property access lock. Default is no timeout.
Example
typeof(TestClass).SetStaticProperty("StaticProperty", "new static value");
// Now the 'StaticProperty' will be set to "new static value"