# 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

### ![](/files/hsR4ok3152WyAf8J2C1u) DynamicObject()

Default constructor.

### ![](/files/hsR4ok3152WyAf8J2C1u) 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. |

### ![](/files/hsR4ok3152WyAf8J2C1u) DynamicObject(source)

Creates a new DynamicObject cloned from the source object.

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

## Properties

### ![](/files/hsR4ok3152WyAf8J2C1u) Count

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

### ![](/files/hsR4ok3152WyAf8J2C1u) IsEmpty

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

### ![](/files/hsR4ok3152WyAf8J2C1u) Item(name)

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

## Methods

### ![](/files/hsR4ok3152WyAf8J2C1u) Clear()

Wipes out the object.

### ![](/files/hsR4ok3152WyAf8J2C1u) Clear(keepCapacity)

Wipes out the object but it keeps the capacity.

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

### ![](/files/hsR4ok3152WyAf8J2C1u) Clone()

Clones the members of the dynamic object.

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

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

### ![](/files/hsR4ok3152WyAf8J2C1u) 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.

### ![](/files/hsR4ok3152WyAf8J2C1u) CopyFrom(source)

Copies all the values from the *source* object.

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

### ![](/files/hsR4ok3152WyAf8J2C1u) 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](/api/wisej.core/general/wisej.core.dynamicobject.md). Itself, this call can be chained.

### ![](/files/hsR4ok3152WyAf8J2C1u) 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](/api/wisej.core/general/wisej.core.dynamicobject.md) | previous values to diff against. |

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

### ![](/files/hsR4ok3152WyAf8J2C1u) 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)

### ![](/files/hsR4ok3152WyAf8J2C1u) 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. |

### ![](/files/hsR4ok3152WyAf8J2C1u) Sort()

Sorts the field by their name.

### ![](/files/hsR4ok3152WyAf8J2C1u) ToJSON(formatted)

Serializes the object to a JSON string.

| Parameter                                      | Type                                                            | Description                            |
| ---------------------------------------------- | --------------------------------------------------------------- | -------------------------------------- |
| **formatted** ![](/files/1vGqtbG2j637vQ4ROWOf) | [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.

### ![](/files/hsR4ok3152WyAf8J2C1u) ToJSON(options)

Serializes the object to a JSON string.

| Parameter   | Type                                                                                   | Description                                                                                                    |
| ----------- | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| **options** | [WisejSerializerOptions](/api/wisej.core/general/wisej.core.wisejserializeroptions.md) | A combination of [WisejSerializerOptions](/api/wisej.core/general/wisej.core.wisejserializeroptions.md) 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.

### ![](/files/hsR4ok3152WyAf8J2C1u) TrimExcess()

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

### ![](/files/hsR4ok3152WyAf8J2C1u) 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** ![](/files/RboPbVYgUdZ2I6eDoFQj) | [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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wisej.com/api/wisej.core/general/wisej.core.dynamicobject.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
