ReflectorSetProperty(Object, String, Object, ReflectionWays, Object) Method

Sets the instance property of an object represented by the specified propertyName.

Definition

Namespace: KGySoft.Reflection
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.1.0
C#
public static void SetProperty(
	Object instance,
	string propertyName,
	Object? value,
	ReflectionWays way,
	params Object?[]? indexParameters
)

Parameters

instance  Object
An instance whose property is about to be set.
propertyName  String
The name of the property to be set.
value  Object
The value to set.
way  ReflectionWays
The preferred reflection way.
indexParameters  Object
Index parameters if propertyName refers to an indexed property. This parameter is ignored for non-indexed properties.

Remarks

propertyName can refer public and non-public properties. To avoid ambiguity (in case of indexers), this method gets all of the properties of the same name and chooses the first one for which the provided indexParameters match.

If you already have a PropertyInfo instance use the SetProperty(Object, PropertyInfo, Object, ReflectionWays, Object) method for better performance.

If you are not sure whether a property with the specified propertyName exists, then you can use the TrySetProperty methods instead.

If way is Auto, then this method uses the TypeDescriptor way for ICustomTypeDescriptor implementations and the DynamicDelegate way otherwise. If the .NET Standard 2.0 version of the KGySoft.CoreLibraries assembly is referenced and the property belongs to a value type (struct), then the SystemReflection way will be used.

  Tip

To preserve the changes of a mutable value type embed it into a variable of Object type and pass it to the instance parameter of this method.

See Also