我猜想它被密封的原因是因为实际的CreateMetadata
实现包含你不应该修改的缓存逻辑。
为了扩展CachedDataAnnotationsModelMetadataProvider
,我发现以下似乎效果很好:
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;
}
}