private static void FillCollectionInfo(Type it, CollectionInfo info, ref int level) { /* * level: * 0 = not a collection * 1 = collection * 2 = list * 3 = generic collection, does not support collection manipulation. * 4 = generic list */ if (it.IsGenericType) { Type def = it.GetGenericTypeDefinition(); if (def == typeof(IList <>)) { info.SupportManipulation = true; info.ElementType = it.GetGenericArguments()[0]; info.InterfaceType = it; info.IsGeneric = true; level = 4; } else if (def == typeof(ICollection <>) && level < 3) { info.SupportManipulation = true; info.ElementType = it.GetGenericArguments()[0]; info.IsGeneric = true; info.InterfaceType = it; level = 3; } } else { if (it == typeof(IList) && level < 2) { info.SupportManipulation = true; info.InterfaceType = it; level = 2; } else if (it == typeof(ICollection) && level < 1) { info.InterfaceType = it; level = 1; } } }
public static CollectionInfo GetCollectionInfo(this Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } CollectionInfo info = new CollectionInfo(type); if (type.IsArray) { info.ElementType = type.GetElementType(); info.IsArray = true; info.InterfaceType = typeof(ICollection); } else { int level = 0; if (type.IsInterface) { FillCollectionInfo(type, info, ref level); } if (level == 0) { foreach (var it in type.GetInterfaces()) { FillCollectionInfo(it, info, ref level); if (level == 4) { break; } } } } return(info); }
internal ModelPropertyInfo(PropertyInfo info, ModelInfo model) { _property = info; _getter = CreateGetterAccessor(info); _setter = CreateSetterAccessor(info); _model = model; _collectionInfo = info.PropertyType.GetCollectionInfo(); _maps = info.GetCustomAttributes <ModelMapAttribute>().ToArray(); _display = info.GetCustomAttribute <DisplayAttribute>(); if (_property.IsDefined(typeof(KeyAttribute))) { _keyType = ModelKeyType.Explicit; } var ca = _property.GetCustomAttribute <ColumnAttribute>(); if (ca != null) { _order = ca.Order; } }