What is the "Func<object> modelAccessor" parameter for in MVC's DataAnnotationsModelMetadataProvider?

Viewed 1784

It's one of the parameters supplied to the CreateMetadata method (which you override if extending metadata support).

ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes,
                             Type containerType,
                             Func<object> modelAccessor, <<--THIS ONE
                             Type modelType,
                             string propertyName)

I had assumed that it allowed you to access the model object itself (e.g. for setting metadata based on model values), however when I try to use it to cast to my model object I just get null.

Entity ent = (Entity)modelAccessor(); // = Null

If I've missunderstood, can anyone explain what it's purpose is? Or alternatively, how to properly use it?

Thanks

2 Answers
Related