> For the complete documentation index, see [llms.txt](https://kinematicsoup.gitbook.io/reactor/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinematicsoup.gitbook.io/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksunityreflectionutils.md).

# Class ksUnityReflectionUtils

Namespace: [KS.Reactor.Client.Unity](/reactor/api-reference/ks.reactor.client.unity.md)\
Assembly: KSUnity-Source.dll

Cloning and reflection utility functions.

```csharp
public class ksUnityReflectionUtils
```

#### Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) â† [ksUnityReflectionUtils](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksunityreflectionutils.md)

#### Inherited Members

[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals\(system-object\)), [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals\(system-object-system-object\)), [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode), [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype), [object.MemberwiseClone()](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone), [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals), [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Methods

### Clone\<T>(T, bool, Filter) <a href="#ks_reactor_client_unity_ksunityreflectionutils_clone__1___0_system_boolean_ks_reactor_client_unity_k" id="ks_reactor_client_unity_ksunityreflectionutils_clone__1___0_system_boolean_ks_reactor_client_unity_k"></a>

Clones an object using reflection.

```csharp
public static T Clone<T>(T src, bool deepCopy = false, ksUnityReflectionUtils.Filter filter = null) where T : class
```

#### Parameters

`src` T

Object to clone.

`deepCopy` [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

If true, referenced objects are also copied. References to Objects never create copies and always reference the original object.

`filter` [ksUnityReflectionUtils](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksunityreflectionutils.md).[Filter](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksunityreflectionutils/ks.reactor.client.unity.ksunityreflectionutils.filter.md)

Optional filter for excluding fields from being copied. If null, all fields are copied.

#### Returns

T

Cloned object.

#### Type Parameters

`T`

Type of the object being cloned.

### Clone(object, bool, Filter) <a href="#ks_reactor_client_unity_ksunityreflectionutils_clone_system_object_system_boolean_ks_reactor_client" id="ks_reactor_client_unity_ksunityreflectionutils_clone_system_object_system_boolean_ks_reactor_client"></a>

Clones an object using reflection.

```csharp
public static object Clone(object src, bool deepCopy = false, ksUnityReflectionUtils.Filter filter = null)
```

#### Parameters

`src` [object](https://learn.microsoft.com/dotnet/api/system.object)

Object to clone.

`deepCopy` [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

If true, referenced objects are also copied. References to Objects never create copies and always reference the original object.

`filter` [ksUnityReflectionUtils](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksunityreflectionutils.md).[Filter](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksunityreflectionutils/ks.reactor.client.unity.ksunityreflectionutils.filter.md)

Optional filter for excluding fields from being copied. If null, all fields are copied.

#### Returns

[object](https://learn.microsoft.com/dotnet/api/system.object)

Cloned object.

### CloneScript\<T>(T, GameObject) <a href="#ks_reactor_client_unity_ksunityreflectionutils_clonescript__1___0_unityengine_gameobject" id="ks_reactor_client_unity_ksunityreflectionutils_clonescript__1___0_unityengine_gameobject"></a>

Creates a copy of a component on another game object using reflection. If the script implements [ksICloneableScript](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksicloneablescript.md), copies it by calling [CopyTo](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksicloneablescript.md#KS_Reactor_Client_Unity_ksICloneableScript_CopyTo_UnityEngine_Component_) instead of using reflection. When using reflection, we try to follow the same rules Unity uses when copying objects with Instantiate, but Unity does not expose this functionality for copying scripts so we had to try recreate their rules and there may be differences.

```csharp
public static T CloneScript<T>(T component, GameObject gameObject) where T : Component
```

#### Parameters

`component` T

Component to copy.

`gameObject` GameObject

Game object to add the copy to.

#### Returns

T

The copied component.

#### Type Parameters

`T`

Type of component to clone.

### Copy\<T>(T, T, bool, Filter) <a href="#ks_reactor_client_unity_ksunityreflectionutils_copy__1___0___0_system_boolean_ks_reactor_client_unit" id="ks_reactor_client_unity_ksunityreflectionutils_copy__1___0___0_system_boolean_ks_reactor_client_unit"></a>

Copies field values from one object onto another using reflection.

```csharp
public static void Copy<T>(T src, T dest, bool deepCopy = false, ksUnityReflectionUtils.Filter filter = null)
```

#### Parameters

`src` T

Object to copy values from.

`dest` T

Object to copy values to.

`deepCopy` [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

If true, referenced objects are also copied. References to Objects never create copies and always reference the original object.

`filter` [ksUnityReflectionUtils](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksunityreflectionutils.md).[Filter](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksunityreflectionutils/ks.reactor.client.unity.ksunityreflectionutils.filter.md)

Optional filter for excluding fields from being copied. If null, all fields are copied.

#### Type Parameters

`T`

Type of the object to copy values from.

### Copy(Type, object, object, bool, Filter) <a href="#ks_reactor_client_unity_ksunityreflectionutils_copy_system_type_system_object_system_object_system_b" id="ks_reactor_client_unity_ksunityreflectionutils_copy_system_type_system_object_system_object_system_b"></a>

Copies field values from one object onto another using reflection.

```csharp
public static void Copy(Type type, object src, object dest, bool deepCopy = false, ksUnityReflectionUtils.Filter filter = null)
```

#### Parameters

`type` [Type](https://learn.microsoft.com/dotnet/api/system.type)

Type of the object being copied. `src` and `dest` must be instances of this type or a derived type. Derived type fields will not be copied.

`src` [object](https://learn.microsoft.com/dotnet/api/system.object)

Object to copy values from.

`dest` [object](https://learn.microsoft.com/dotnet/api/system.object)

Object to copy values to.

`deepCopy` [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

If true, referenced objects are also copied. References to Objects never create copies and always reference the original object.

`filter` [ksUnityReflectionUtils](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksunityreflectionutils.md).[Filter](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksunityreflectionutils/ks.reactor.client.unity.ksunityreflectionutils.filter.md)

Optional filter for excluding fields from being copied. If null, all fields are copied.

#### Exceptions

[ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception)

Thrown if `type`, `src`, or `dest` are null.

[ArgumentException](https://learn.microsoft.com/dotnet/api/system.argumentexception)

Thrown if `src` or `dest` are not instances of `type` or a derived type.

### IsUnitySerialized(FieldInfo) <a href="#ks_reactor_client_unity_ksunityreflectionutils_isunityserialized_system_reflection_fieldinfo" id="ks_reactor_client_unity_ksunityreflectionutils_isunityserialized_system_reflection_fieldinfo"></a>

Checks if a field will be serialized by Unity. Unity does not expose a function for this, so we've tried to recreate Unity's functionality. It may not be 100% accurate.

```csharp
public static bool IsUnitySerialized(FieldInfo field)
```

#### Parameters

`field` [FieldInfo](https://learn.microsoft.com/dotnet/api/system.reflection.fieldinfo)

Field to check.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if Unity will serialize the field.

### IsUnitySerialized(Type) <a href="#ks_reactor_client_unity_ksunityreflectionutils_isunityserialized_system_type" id="ks_reactor_client_unity_ksunityreflectionutils_isunityserialized_system_type"></a>

Checks if a type will be serialized by Unity. Unity does not expose a function for this, so we've tried to recreate Unity's functionality. It may not be 100% accurate.

```csharp
public static bool IsUnitySerialized(Type type)
```

#### Parameters

`type` [Type](https://learn.microsoft.com/dotnet/api/system.type)

Type to check.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if Unity will serialize the type.
