ReflectorTrySetField(Object, String, Boolean, Object, ReflectionWays) Method

Tries to set the instance field of an object represented by the specified fieldName.

Definition

Namespace: KGySoft.Reflection
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 10.0.0
C#
public static bool TrySetField(
	Object instance,
	string fieldName,
	bool ignoreCase,
	Object? value,
	ReflectionWays way = ReflectionWays.Auto
)

Parameters

instance  Object
An instance whose field is about to be set.
fieldName  String
The name of the field to be set.
ignoreCase  Boolean
to ignore case; to regard case.
value  Object
The value to set.
way  ReflectionWays  (Optional)
The preferred reflection way. TypeDescriptor way is not applicable for fields. This parameter is optional.
Default value: Auto.

Return Value

Boolean
, if the field could be set; , if a field with name fieldName could not be found.

Remarks

fieldName can refer public and non-public fields.

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

If way is Auto, then the DynamicDelegate 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