// ReSharper disable once UnusedMember.Local private static IClrCollectionAccessor CreateGeneric <TEntity, TCollection, TElement>(PropertyInfo property) where TEntity : class where TCollection : class, ICollection <TElement> { var getterDelegate = (Func <TEntity, TCollection>)property.GetMethod.CreateDelegate(typeof(Func <TEntity, TCollection>)); Action <TEntity, TCollection> setterDelegate = null; Func <TEntity, Action <TEntity, TCollection>, TCollection> createAndSetDelegate = null; Func <TCollection> createDelegate = null; var setter = property.SetMethod; if (setter != null) { setterDelegate = (Action <TEntity, TCollection>)setter.CreateDelegate(typeof(Action <TEntity, TCollection>)); var concreteType = new CollectionTypeFactory().TryFindTypeToInstantiate(typeof(TCollection)); if (concreteType != null) { createAndSetDelegate = (Func <TEntity, Action <TEntity, TCollection>, TCollection>)_createAndSet .MakeGenericMethod(typeof(TEntity), typeof(TCollection), concreteType) .CreateDelegate(typeof(Func <TEntity, Action <TEntity, TCollection>, TCollection>)); createDelegate = (Func <TCollection>)_create .MakeGenericMethod(typeof(TCollection), concreteType) .CreateDelegate(typeof(Func <TCollection>)); } } return(new ClrICollectionAccessor <TEntity, TCollection, TElement>( property.Name, getterDelegate, setterDelegate, createAndSetDelegate, createDelegate)); }
public void Returns_HashSet_if_assignable() { var factory = new CollectionTypeFactory(); Assert.Same(typeof(HashSet <Random>), factory.TryFindTypeToInstantiate(typeof(ICollection <Random>))); Assert.Same(typeof(HashSet <Random>), factory.TryFindTypeToInstantiate(typeof(ISet <Random>))); }
public void Returns_given_type_if_public_parameterless_constructor_available() { var factory = new CollectionTypeFactory(); Assert.Same(typeof(CustomHashSet), factory.TryFindTypeToInstantiate(typeof(CustomHashSet))); Assert.Same(typeof(CustomList), factory.TryFindTypeToInstantiate(typeof(CustomList))); Assert.Same(typeof(HashSet <Random>), factory.TryFindTypeToInstantiate(typeof(HashSet <Random>))); Assert.Same(typeof(List <Random>), factory.TryFindTypeToInstantiate(typeof(List <Random>))); Assert.Same(typeof(ObservableCollection <Random>), factory.TryFindTypeToInstantiate(typeof(ObservableCollection <Random>))); }
public void Returns_null_when_no_usable_concrete_type_found() { var factory = new CollectionTypeFactory(); Assert.Null(factory.TryFindTypeToInstantiate(typeof(PrivateConstructor))); Assert.Null(factory.TryFindTypeToInstantiate(typeof(InternalConstructor))); Assert.Null(factory.TryFindTypeToInstantiate(typeof(ProtectedConstructor))); Assert.Null(factory.TryFindTypeToInstantiate(typeof(NoParameterlessConstructor))); Assert.Null(factory.TryFindTypeToInstantiate(typeof(Abstract))); Assert.Null(factory.TryFindTypeToInstantiate(typeof(object))); Assert.Null(factory.TryFindTypeToInstantiate(typeof(Random))); Assert.Null(factory.TryFindTypeToInstantiate(typeof(IEnumerable <Random>))); }