Skip to main content
All docs
V24.2

NewItemInstanceInitializerAttribute.CreateInstance(ITypeDescriptorContext, IEnumerable) Method

Creates a dictionary item with a key/value pair.

Namespace: DevExpress.Mvvm.DataAnnotations

Assembly: DevExpress.Mvvm.v24.2.dll

Declaration

public virtual KeyValuePair<object, object>? CreateInstance(
    ITypeDescriptorContext context,
    IEnumerable dictionary
)

Parameters

Name Type Description
context ITypeDescriptorContext

Context information about a new dictionary item to be created.

dictionary IEnumerable

A new dictionary item to be created.

Returns

Type Description
Nullable<KeyValuePair<Object, Object>>

A KeyValuePair object that contains the key/value pair.

Remarks

This method allows you to create a new dictionary item with a key/value pair. The default implementation returns null.

In the following code sample, the CreateInstance method overrides the base implementation to create a new dictionary item where the key is a unique integer string and the value is an instance of ItemInitializer.

public class DictionaryKey1 : NewItemInstanceInitializerAttribute {
    public DictionaryKey1(Type type) : base(type) { }
    public override KeyValuePair<object, object>? CreateInstance(ITypeDescriptorContext context, IEnumerable dictionary) {
        var testObject = ((DescriptorContext)context).Value as IDictionary<string, object>;
        int key = testObject.Keys.Count;
        while(testObject.Keys.Contains(key.ToString()))
            key++;
        return new KeyValuePair<object, object>(key.ToString(), new ConcreteClass());
    }
}
See Also