1 namespace System.Web.ModelBinding {
2     using System;
3 
4     // Returns a user-specified binder for a given type.
5     public sealed class SimpleModelBinderProvider : ModelBinderProvider {
6 
7         private readonly Func<IModelBinder> _modelBinderFactory;
8         private readonly Type _modelType;
9 
SimpleModelBinderProvider(Type modelType, IModelBinder modelBinder)10         public SimpleModelBinderProvider(Type modelType, IModelBinder modelBinder) {
11             if (modelType == null) {
12                 throw new ArgumentNullException("modelType");
13             }
14             if (modelBinder == null) {
15                 throw new ArgumentNullException("modelBinder");
16             }
17 
18             _modelType = modelType;
19             _modelBinderFactory = () => modelBinder;
20         }
21 
SimpleModelBinderProvider(Type modelType, Func<IModelBinder> modelBinderFactory)22         public SimpleModelBinderProvider(Type modelType, Func<IModelBinder> modelBinderFactory) {
23             if (modelType == null) {
24                 throw new ArgumentNullException("modelType");
25             }
26             if (modelBinderFactory == null) {
27                 throw new ArgumentNullException("modelBinderFactory");
28             }
29 
30             _modelType = modelType;
31             _modelBinderFactory = modelBinderFactory;
32         }
33 
34         public Type ModelType {
35             get {
36                 return _modelType;
37             }
38         }
39 
40         public bool SuppressPrefixCheck {
41             get;
42             set;
43         }
44 
GetBinder(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext)45         public override IModelBinder GetBinder(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext) {
46             ModelBinderUtil.ValidateBindingContext(bindingContext);
47 
48             if (bindingContext.ModelType == ModelType) {
49                 if (SuppressPrefixCheck || bindingContext.UnvalidatedValueProvider.ContainsPrefix(bindingContext.ModelName)) {
50                     return _modelBinderFactory();
51                 }
52             }
53 
54             return null;
55         }
56 
57     }
58 }
59