private static bool TryGetProviderFromAttributes(Type modelType, out ModelBinderProvider provider) { ExtensibleModelBinderAttribute attr = TypeDescriptorHelper.Get(modelType).GetAttributes().OfType <ExtensibleModelBinderAttribute>().FirstOrDefault(); if (attr == null) { provider = null; return(false); } if (typeof(ModelBinderProvider).IsAssignableFrom(attr.BinderType)) { provider = (ModelBinderProvider)Activator.CreateInstance(attr.BinderType); } else if (typeof(IExtensibleModelBinder).IsAssignableFrom(attr.BinderType)) { Type closedBinderType = (attr.BinderType.IsGenericTypeDefinition) ? attr.BinderType.MakeGenericType(modelType.GetGenericArguments()) : attr.BinderType; IExtensibleModelBinder binderInstance = (IExtensibleModelBinder)Activator.CreateInstance(closedBinderType); provider = new SimpleModelBinderProvider(modelType, binderInstance) { SuppressPrefixCheck = attr.SuppressPrefixCheck }; } else { string errorMessage = String.Format(CultureInfo.CurrentCulture, MvcResources.ModelBinderProviderCollection_InvalidBinderType, attr.BinderType, typeof(ModelBinderProvider), typeof(IExtensibleModelBinder)); throw new InvalidOperationException(errorMessage); } return(true); }
internal void RegisterBinderForType(Type modelType, IExtensibleModelBinder modelBinder, bool suppressPrefixCheck) { SimpleModelBinderProvider provider = new SimpleModelBinderProvider(modelType, modelBinder) { SuppressPrefixCheck = suppressPrefixCheck }; InsertSimpleProviderAtFront(provider); }