FieldAccessorSet Method

Sets the field. For static fields the instance parameter is omitted (can be ).

Definition

Namespace: KGySoft.Reflection
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.1.0
C#
public void Set(
	Object? instance,
	Object? value
)

Parameters

instance  Object
The instance that the field belongs to. Can be for static fields.
value  Object
The value to set.

Remarks

Setting the field for the first time is slower than the System.Reflection.FieldInfo.SetValue method but further calls are much faster.

  Tip

If you know the type of the field at compile time (and also the declaring type for instance fields), then you can use the generic SetStaticValue or SetInstanceValue methods for better performance.

  Notes to Callers

If the field is read-only or is an instance field of a value type, then the .NET Standard 2.0 version of this method defaults to use regular reflection to preserve mutations. To experience the best performance try to target .NET Standard 2.1 or any .NET Framework or .NET Core/.NET platforms instead.

Exceptions

InvalidOperationExceptionThis FieldAccessor represents a constant field or a field of an open generic type.
ArgumentNullExceptionThis FieldAccessor represents an instance field and instance is
-or-
This FieldAccessor represents a value type field and value is .
ArgumentExceptionThe type of instance or value is invalid.
NotSupportedExceptionOn .NET Framework the code is executed in a partially trusted domain with insufficient permissions.
PlatformNotSupportedExceptionYou use the .NET Standard 2.0 build of KGySoft.CoreLibraries and this FieldAccessor represents a read-only field.

See Also