Skip to content

Fields

The ByteCobra.Reflection.FieldExtensions class provides extension methods for working with fields via reflection, with thread safety. This allows you to get and set field values on object instances and types (including static fields) with the guarantee that the access is thread-safe, avoiding potential race conditions.

Methods

GetField<T>

Gets the value of an instance field from the specified object.

Parameters

  • classInstance: The object instance from which to get the field value.
  • name: The name of the field.
  • flags: Optional. The binding flags used in the field lookup. Default is BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic.
  • timeout: Optional. The maximum time to wait for the field access lock. Default is no timeout.

Returns: The value of the field if found and accessible; otherwise, the default value of the type T.

Example

public class TestClass
{
    public string InstanceField;
    public static string StaticField;
}

var testInstance = new TestClass { InstanceField = "test value" };
var value = testInstance.GetField<string>("InstanceField");
// value will be "test value"

SetField<T>

Sets the value of an instance field on the specified object.

Parameters

  • classInstance: The object instance on which to set the field value.
  • name: The name of the field.
  • value: The value to set for the field.
  • flags: Optional. The binding flags used in the field lookup. Default is BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic.
  • timeout: Optional. The maximum time to wait for the field access lock. Default is no timeout.

Example

var testInstance = new TestClass();
testInstance.SetField("InstanceField", "new value");
// Now the 'InstanceField' will be set to "new value"

GetStaticField<T>

Gets the value of a static field from the specified type.

Parameters

  • type: The type containing the static field.
  • name: The name of the static field to retrieve.
  • flags: Optional. The binding flags used in the field lookup. Default is BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic.
  • timeout: Optional. The maximum time to wait for the field access lock. Default is no timeout.

Returns: The value of the static field if found and accessible; otherwise, the default value of the type T.

Example

TestClass.StaticField = "static test value";
var value = typeof(TestClass).GetStaticField<string>("StaticField");
// value will be "static test value"

SetStaticField<T>

Sets the value of a static field on the specified type.

Parameters

  • type: The type containing the static field.
  • name: The name of the static field to set.
  • value: The value to set for the static field.
  • flags: Optional. The binding flags used in the field lookup. Default is BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic.
  • timeout: Optional. The maximum time to wait for the field access lock. Default is no timeout.

Example

typeof(TestClass).SetStaticField("StaticField", "new static value");
// Now the 'StaticField' will be set to "new static value"