I am working on a small library for ASP.NET MVC 3 that should offer the best reuse of model metadata and easy matching from data entities from / to custom viewmodels. To do this, I need to provide my own implementation of ICustomTypeDescriptor for three different areas of interest in ASP.NET MVC:
- Scaffolding
- Check
- Modelbinding
It seems like you can do this by installing System.Web.Mvc.ModelMetadataProviders.Current on my own CustomMetaDataProvider, but this is not enough to cover all three points above.
The problem is that there are several classes in System.Web.Mvc that call directly to this System.Web.TypeDescriptorHelper , which does not expand, because it looks like this:
internal static class TypeDescriptorHelper { public static ICustomTypeDescriptor Get(Type type) { return new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type); } }
The only solution I found is very inconvenient and requires subclassing the many types from System.Web.Mvc to make it work. I even had to fully implement CustomModelBinderDictionary just to rewrite one or two lines of code. This way it works, but it is a very dirty hack and will probably break the next time I upgrade to a new version of ASP.NET MVC.
So here is what I like to know . Did I skip any easy way to do this?
Bonus question . If not, and you are from the MVC team, can you consider creating an appropriate extension point in MVC 4; -)?
Edit: In response to the question why I need to encode my own TypeDescriptor: there are several reasons for this: 1. Most important: I need a workaround for the problem described in https://forums.asp.net/t/1614439.aspx/ 1 2. I also need to dynamically insert metadata for various reasons. For example, I want to encode my own Bind attribute, but BindAttribute is sealed. So instead of flowing out of it, I dynamically generate the corresponding BindAttribute from the TypeDescriptor when it detects my own implementation of the binding attribute.
source share