ASP.NET Dynamic Data - Tips and Tricks #3

You've created a custom attribute to use in your Dynamic Data application that has the AllowMultiple property set to true. When you read the MetaColumn.Attributes property (if the attribute is applied to a data field) or you read the MetaTable.Attributes property (if the attribute is applied to a table), you don't see the multiple attributes of the same type in the list, only the first one is returned.

Why?

The TypeDescriptionProvider class internally constructs a collection of attributes and filters out identical attributes. It determines whether an attribute is identical or not by examining the TypeId property. By default, the TypeId property returns the type of the attribute. Since both attributes are of the same type, only the first one is retrieved and the remaining ones are considered identical.

To fix this problem, you need a way to distinguish each attribute instance. The easiest way to do that is to override the TypeId property in your custom attribute implementation and simply return the current instance, like the following:

 public override object TypeId
{
  get
  {
    return this;
  }
}