public static bool IsICollectionType(Type collectionType) { var genericEnumerableType = typeof(ICollection <>); if (collectionType.GenericTypeArguments.Count() != 1) { return(false); } var elementType = collectionType.GenericTypeArguments.Single(); var expectedType = genericEnumerableType.MakeGenericType(new [] { elementType }); if (!expectedType.IsAssignableFrom(collectionType)) { return(false); } var constructor = collectionType.GetConstructor(new Type[0]); if (constructor == null) { return(false); } _collectionsInfos[collectionType] = new GenericCollectionInfo(collectionType, elementType, constructor); return(true); }
public static GenericCollectionInfo GetCollectionInfo(Type collectionType) { if (!_collectionsInfos.ContainsKey(collectionType)) { var elementType = collectionType.GenericTypeArguments.Single(); var constructor = collectionType.GetConstructor(new Type[0]); _collectionsInfos[collectionType] = new GenericCollectionInfo(collectionType, elementType, constructor); } return(_collectionsInfos[collectionType]); }