1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
namespace System.Web.ModelBinding {
using System;
using System.Collections;
using System.Globalization;
using System.Linq;
internal static class ModelBinderUtil {
public static TModel CastOrDefault<TModel>(object model) {
return (model is TModel) ? (TModel)model : default(TModel);
}
public static string CreateIndexModelName(string parentName, int index) {
return CreateIndexModelName(parentName, index.ToString(CultureInfo.InvariantCulture));
}
public static string CreateIndexModelName(string parentName, string index) {
return (parentName.Length == 0) ? "[" + index + "]" : parentName + "[" + index + "]";
}
public static string CreatePropertyModelName(string prefix, string propertyName) {
if (String.IsNullOrEmpty(prefix)) {
return propertyName ?? String.Empty;
}
else if (String.IsNullOrEmpty(propertyName)) {
return prefix ?? String.Empty;
}
else {
return prefix + "." + propertyName;
}
}
public static IModelBinder GetPossibleBinderInstance(Type closedModelType, Type openModelType, Type openBinderType) {
Type[] typeArguments = TypeHelpers.GetTypeArgumentsIfMatch(closedModelType, openModelType);
return (typeArguments != null) ? (IModelBinder)Activator.CreateInstance(openBinderType.MakeGenericType(typeArguments)) : null;
}
public static object[] RawValueToObjectArray(object rawValue) {
// precondition: rawValue is not null
// Need to special-case String so it's not caught by the IEnumerable check which follows
if (rawValue is string) {
return new object[] { rawValue };
}
object[] rawValueAsObjectArray = rawValue as object[];
if (rawValueAsObjectArray != null) {
return rawValueAsObjectArray;
}
IEnumerable rawValueAsEnumerable = rawValue as IEnumerable;
if (rawValueAsEnumerable != null) {
return rawValueAsEnumerable.Cast<object>().ToArray();
}
// fallback
return new object[] { rawValue };
}
public static void ReplaceEmptyStringWithNull(ModelMetadata modelMetadata, ref object model) {
if (modelMetadata.ConvertEmptyStringToNull && StringIsEmptyOrWhitespace(model as string)) {
model = null;
}
}
// Based on String.IsNullOrWhitespace
private static bool StringIsEmptyOrWhitespace(string s) {
if (s == null) {
return false;
}
if (s.Length != 0) {
for (int i = 0; i < s.Length; i++) {
if (!Char.IsWhiteSpace(s[i])) {
return false;
}
}
}
return true;
}
public static void ValidateBindingContext(ModelBindingContext bindingContext) {
if (bindingContext == null) {
throw new ArgumentNullException("bindingContext");
}
if (bindingContext.ModelMetadata == null) {
throw Error.ModelBinderUtil_ModelMetadataCannotBeNull();
}
}
public static void ValidateBindingContext(ModelBindingContext bindingContext, Type requiredType, bool allowNullModel) {
ValidateBindingContext(bindingContext);
if (bindingContext.ModelType != requiredType) {
throw Error.ModelBinderUtil_ModelTypeIsWrong(bindingContext.ModelType, requiredType);
}
if (!allowNullModel && bindingContext.Model == null) {
throw Error.ModelBinderUtil_ModelCannotBeNull(requiredType);
}
if (bindingContext.Model != null && !requiredType.IsInstanceOfType(bindingContext.Model)) {
throw Error.ModelBinderUtil_ModelInstanceIsWrong(bindingContext.Model.GetType(), requiredType);
}
}
}
}
|