I would guess that the reason it is sealed is because the actual CreateMetadata
implementation contains caching logic that you should not be modifying.
To extend CachedDataAnnotationsModelMetadataProvider
, I find that the following seems to work well:
using System.Web.Mvc;
public class MyCustomMetadataProvider : CachedDataAnnotationsModelMetadataProvider
{
protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
{
var result = base.CreateMetadataFromPrototype(prototype, modelAccessor);
//modify the base result with your custom logic, typically adding items from
//prototype.AdditionalValues, e.g.
result.AdditionalValues.Add("MyCustomValuesKey", prototype.AdditionalValues["MyCustomValuesKey"]);
return result;
}
protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable<Attribute> attributes, Type containerType, Type modelType, string propertyName)
{
CachedDataAnnotationsModelMetadata prototype = base.CreateMetadataPrototype(attributes, containerType, modelType, propertyName);
//Add custom prototype data, e.g.
prototype.AdditionalValues.Add("MyCustomValuesKey", "MyCustomValuesData");
return prototype;
}
}