The following code snippets illustrate the usage:

PropertyTypeInt32 propertyTypeId;
PropertyTypeString propertyTypeCode;
PropertyTypeString propertyTypeDescription;
PropertyTypeBool propertyTypeActive;

// define the entity type and it's properties
DynamicType<object> dynamicType = new DynamicType<object>
    propertyTypeId = new PropertyTypeInt32("Id"),
    propertyTypeCode = new PropertyTypeString("Code"),
    propertyTypeDescription = new PropertyTypeString("Description"),
    propertyTypeActive = new PropertyTypeBool("Active", true)

// create the instance factory
IDynamicTypeFactory<object> factory = dynamicType.CreateFactory();

// create the instance factory
object instance = factory.CreateInstance();

// assign a property
instance.SetProperty(propertyTypeId, 2);

// retrieve property values
int id = instance.GetProperty(propertyTypeId);
string code = instance.GetProperty(propertyTypeCode);
string description = instance.GetProperty(propertyTypeDescription);
bool active = instance.GetProperty(propertyTypeActive);

Also, interfaces can be used to allow for scenarios where some of the property types are always required, or are part of the domain.
The requirement is that the interface only has properties and their names and data types correspond to some (or all) of the property types supplied at runtime.

Given the following interface:
public interface INamedItem
  string Name { get; set; }

The following code snippet illustrates how it ties in:

PropertyTypeString propertyTypeDescription;

// define the entity type and it's properties
DynamicType<INamedItem> dynamicType = new DynamicType<INamedItem>
    new PropertyTypeString("Name"),
    propertyTypeDescription = new PropertyTypeString("Description")

// create the instance factory
IDynamicTypeFactory<INamedItem> factory = dynamicType.CreateFactory();

// create a new instance
INamedItem instance = factory.CreateInstance();

// now the "dynamic" property is directly accessible
instance.Name = "ABC";

// "unknown" properties WRT the interface are still accessible!
instance.SetProperty(propertyTypeDescription, "You there?");
string description = instance.GetProperty(propertyTypeDescription);

Further examples to follow!

Last edited Nov 15, 2008 at 6:30 PM by VirtualStaticVoid, version 3


No comments yet.