ASP.NET没有魔法——ASP.NET MVC 模型绑定解析(下篇) (4)

ASP.NET没有魔法——ASP.NET MVC 模型绑定解析(下篇)

ASP.NET没有魔法——ASP.NET MVC 模型绑定解析(下篇)

1 internal object BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 2 { 3 object model = bindingContext.Model; 4 Type modelType = bindingContext.ModelType; 5 if (model == null && modelType.IsArray) 6 { 7 Type elementType = modelType.GetElementType(); 8 Type modelType2 = typeof(List<>).MakeGenericType(new Type[] 9 { 10 elementType 11 }); 12 object collection = this.CreateModel(controllerContext, bindingContext, modelType2); 13 ModelBindingContext bindingContext2 = new ModelBindingContext 14 { 15 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => collection, modelType2), 16 ModelName = bindingContext.ModelName, 17 ModelState = bindingContext.ModelState, 18 PropertyFilter = bindingContext.PropertyFilter, 19 ValueProvider = bindingContext.ValueProvider 20 }; 21 IList list = (IList)this.UpdateCollection(controllerContext, bindingContext2, elementType); 22 if (list == null) 23 { 24 return null; 25 } 26 Array array = Array.CreateInstance(elementType, list.Count); 27 list.CopyTo(array, 0); 28 return array; 29 } 30 else 31 { 32 if (model == null) 33 { 34 model = this.CreateModel(controllerContext, bindingContext, modelType); 35 } 36 Type type = TypeHelpers.ExtractGenericInterface(modelType, typeof(IDictionary<, >)); 37 if (type != null) 38 { 39 Type[] genericArguments = type.GetGenericArguments(); 40 Type keyType = genericArguments[0]; 41 Type valueType = genericArguments[1]; 42 ModelBindingContext modelBindingContext = new ModelBindingContext(); 43 modelBindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, modelType); 44 modelBindingContext.ModelName = bindingContext.ModelName; 45 modelBindingContext.ModelState = bindingContext.ModelState; 46 modelBindingContext.PropertyFilter = bindingContext.PropertyFilter; 47 modelBindingContext.ValueProvider = bindingContext.ValueProvider; 48 ModelBindingContext bindingContext3 = modelBindingContext; 49 return this.UpdateDictionary(controllerContext, bindingContext3, keyType, valueType); 50 } 51 Type type2 = TypeHelpers.ExtractGenericInterface(modelType, typeof(IEnumerable<>)); 52 if (type2 != null) 53 { 54 Type type3 = type2.GetGenericArguments()[0]; 55 Type type4 = typeof(ICollection<>).MakeGenericType(new Type[] 56 { 57 type3 58 }); 59 if (type4.IsInstanceOfType(model)) 60 { 61 ModelBindingContext modelBindingContext2 = new ModelBindingContext(); 62 modelBindingContext2.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, modelType); 63 modelBindingContext2.ModelName = bindingContext.ModelName; 64 modelBindingContext2.ModelState = bindingContext.ModelState; 65 modelBindingContext2.PropertyFilter = bindingContext.PropertyFilter; 66 modelBindingContext2.ValueProvider = bindingContext.ValueProvider; 67 ModelBindingContext bindingContext4 = modelBindingContext2; 68 return this.UpdateCollection(controllerContext, bindingContext4, type3); 69 } 70 } 71 this.BindComplexElementalModel(controllerContext, bindingContext, model); 72 return model; 73 } 74 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpjjyg.html