# DynamicObject

Namespace: **Wisej.Core**

Assembly: **Wisej.Framework** (4.0.0.0)

Custom fast implementation of a dynamic object that can handle the JSON serialization and deserialization. This class implements IDynamicMetaObjectProvider allowing the DLR to compile the dynamic member access on the fly. We could have derived from System.Dynamic.Dynamic but the implementation in the .NET framework uses exceptions to manage custom properties.

{% tabs %}
{% tab title="C#" %}

```csharp
public class DynamicObject : IDynamicMetaObjectProvider, ICloneable, IList<Member>, ICollection<Member>, IEnumerable<Member>, IEnumerable, ISerializable, ICustomTypeDescriptor, INotifyPropertyChanged, IConvertible
```

{% endtab %}

{% tab title="VB.NET" %}

```visual-basic
Public Class DynamicObject
    Inherits IDynamicMetaObjectProvider
    Implements ICloneable, IList(Of Member), ICollection(Of Member), IEnumerable(Of Member), IEnumerable, ISerializable, ICustomTypeDescriptor, INotifyPropertyChanged, IConvertible
```

{% endtab %}
{% endtabs %}

## Constructors

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) DynamicObject()

Default constructor.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) DynamicObject(capacity)

Optional constructor. Takes the initial capacity to allocate the specified number of slots in the store.

| Name         | Type                                                        | Description                      |
| ------------ | ----------------------------------------------------------- | -------------------------------- |
| **capacity** | [Int32](https://docs.microsoft.com/dotnet/api/system.int32) | Initial number of store entries. |

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) DynamicObject(source)

Creates a new DynamicObject cloned from the source object.

| Name       | Type                                                                                    | Description                            |
| ---------- | --------------------------------------------------------------------------------------- | -------------------------------------- |
| **source** | [DynamicObject](https://docs.wisej.com/api/wisej.core/general/wisej.core.dynamicobject) | Source object to copy the fields from. |

## Properties

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) Count

[Int32](https://docs.microsoft.com/dotnet/api/system.int32): Returns the number of fields.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) IsEmpty

[Boolean](https://docs.microsoft.com/dotnet/api/system.boolean): Returns true if the dynamic doesn't object contains any field.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) Item(name)

[Object](https://docs.microsoft.com/dotnet/api/system.object): Returns or sets the value of the specified member.

## Methods

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) Clear()

Wipes out the object.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) Clear(keepCapacity)

Wipes out the object but it keeps the capacity.

| Parameter        | Type                                                            | Description |
| ---------------- | --------------------------------------------------------------- | ----------- |
| **keepCapacity** | [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean) |             |

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) Clone()

Clones the members of the dynamic object.

**Returns:** [DynamicObject](https://docs.wisej.com/api/wisej.core/general/wisej.core.dynamicobject). The cloned instance.

This is a shallow copy. Reference members are copied by reference.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) Contains(name)

Returns whether the object contains the specified field.

| Parameter | Type                                                          | Description                 |
| --------- | ------------------------------------------------------------- | --------------------------- |
| **name**  | [String](https://docs.microsoft.com/dotnet/api/system.string) | Name of the field to check. |

**Returns:** [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean). True if the field *name* exists in the store.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) CopyFrom(source)

Copies all the values from the *source* object.

| Parameter  | Type                                                                                    | Description                  |
| ---------- | --------------------------------------------------------------------------------------- | ---------------------------- |
| **source** | [DynamicObject](https://docs.wisej.com/api/wisej.core/general/wisej.core.dynamicobject) | Object to copy members from. |

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) Delete(name)

Deletes the field.

| Parameter | Type                                                          | Description                  |
| --------- | ------------------------------------------------------------- | ---------------------------- |
| **name**  | [String](https://docs.microsoft.com/dotnet/api/system.string) | Name of the field to remove. |

**Returns:** [DynamicObject](https://docs.wisej.com/api/wisej.core/general/wisej.core.dynamicobject). Itself, this call can be chained.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) Diff(original)

Returns a new ComponentConfiguration object containing only the values that don't exist in the ComponentConfiguration object passed to the method.

| Parameter    | Type                                                                                    | Description                      |
| ------------ | --------------------------------------------------------------------------------------- | -------------------------------- |
| **original** | [DynamicObject](https://docs.wisej.com/api/wisej.core/general/wisej.core.dynamicobject) | previous values to diff against. |

**Returns:** [Object](https://docs.microsoft.com/dotnet/api/system.object). Returns null if there are no differences.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) GetEnumerator()

Returns an enumerator that iterates through the collection.

**Returns:** [IEnumerator\<Member>](https://docs.microsoft.com/dotnet/api/system.collections.generic.ienumerator-1). A [IEnumerator](https://docs.microsoft.com/dotnet/api/system.collections.generic.ienumerator-1)

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) Notify(name)

Fires the [PropertyChanged](https://docs.microsoft.com/dotnet/api/system.componentmodel.inotifypropertychanged.propertychanged) event.

| Parameter | Type                                                          | Description                                                  |
| --------- | ------------------------------------------------------------- | ------------------------------------------------------------ |
| **name**  | [String](https://docs.microsoft.com/dotnet/api/system.string) | Name of the property that has changed in the dynamic object. |

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) Sort()

Sorts the field by their name.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) ToJSON(formatted)

Serializes the object to a JSON string.

| Parameter                                                                                                                                                                                                                   | Type                                                            | Description                            |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- | -------------------------------------- |
| **formatted** ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-723b640963c6cd781ff8c5ce5b24149fa0af7b9c%2Fbadge-optional.svg?alt=media) | [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean) | True to format the output JSON string. |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string). A [String](https://docs.microsoft.com/dotnet/api/system.string) representing the serialized object.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) ToJSON(options)

Serializes the object to a JSON string.

| Parameter   | Type                                                                                                      | Description                                                                                                                       |
| ----------- | --------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| **options** | [WisejSerializerOptions](https://docs.wisej.com/api/wisej.core/general/wisej.core.wisejserializeroptions) | A combination of [WisejSerializerOptions](https://docs.wisej.com/api/wisej.core/general/wisej.core.wisejserializeroptions) flags. |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string). A [String](https://docs.microsoft.com/dotnet/api/system.string) representing the serialized object.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) TrimExcess()

Removes the excess entries in the inner array and in all child fields, recursively.

### ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-2389c55cd19719a73a5ae98e1528c8dc8525cc35%2Finstance.png?alt=media) TryGetValue(name, value)

Tries to retrieve the value for the specified member.

| Parameter                                                                                                                                                                                                            | Type                                                          | Description                                 |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- | ------------------------------------------- |
| **name**                                                                                                                                                                                                             | [String](https://docs.microsoft.com/dotnet/api/system.string) | Name of the field to retrieve.              |
| **value** ![](https://1075938901-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HvlWXJQMV7DxhGzw7Y1%2Fuploads%2Fgit-blob-912a3bc068ac1c69bcaa58145e4ee468ccd8a555%2Fbadge-byref.svg?alt=media) | [Object](https://docs.microsoft.com/dotnet/api/system.object) | Will receive the value of the *name* field. |

**Returns:** [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean). True if the member was found, otherwise false.
