using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace DynamicDescriptors;
///
/// Facilitates the creation of custom type descriptors.
///
public static class DynamicDescriptor
{
///
/// Returns a new instance that will supply
/// dynamic custom type information for the specified object.
///
///
/// The type of the object for which dynamic custom type information will be supplied.
///
///
/// The object for which dynamic custom type information will be supplied.
///
///
/// A new instance that will supply dynamic
/// custom type information for the specified object.
///
public static DynamicTypeDescriptor CreateFromInstance(T instance)
{
if (instance == null)
{
throw new ArgumentNullException(nameof(instance));
}
var provider = TypeDescriptor.GetProvider(instance);
var descriptor = provider.GetTypeDescriptor(instance);
return new DynamicTypeDescriptor(descriptor);
}
///
/// Returns a new instance that wraps the specified
/// instance.
///
///
/// The instance to wrap.
///
///
/// A new instance that wraps the specified
/// instance.
///
public static DynamicTypeDescriptor CreateFromDescriptor(ICustomTypeDescriptor descriptor)
{
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
return new DynamicTypeDescriptor(descriptor);
}
///
/// Returns a new instance that exposes properties
/// defined by the data present in the specified dictionary.
///
/// A dictionary mapping property names to property values.
///
/// A new instance that exposes properties defined by
/// the data present in the specified dictionary.
///
public static DynamicTypeDescriptor CreateFromDictionary(IDictionary data)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
var descriptor = new DictionaryTypeDescriptor(data);
return new DynamicTypeDescriptor(descriptor);
}
///
/// Returns a new instance that exposes properties
/// defined by the data present in the specified dictionary.
///
/// A dictionary mapping property names to property values.
/// A dictionary mapping property names to property types.
///
/// A new instance that exposes properties defined
/// by the data present in the specified dictionary.
///
public static DynamicTypeDescriptor CreateFromDictionary(IDictionary data, IDictionary types)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
var descriptor = new DictionaryTypeDescriptor(data, types);
return new DynamicTypeDescriptor(descriptor);
}
}