public override IEnumerable <TestCaseParameter> CreateInstances(Type targetType) { System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions(); foreach (MethodInfo method in ReflectionHelper.GetMethods(this.FactoryType, typeof(FactoryAttribute))) { if (method.GetParameters().Length > 0) { continue; } FactoryAttribute factoryAttribute = ReflectionHelper.GetAttribute <FactoryAttribute>(method); // the method returns the type or a enumerable collection if (targetType.IsAssignableFrom(method.ReturnType)) { yield return(CreateSingleInstance(method)); } else if ( targetType.IsAssignableFrom(typeof(IEnumerable <>).MakeGenericType(targetType)) || ( targetType.IsAssignableFrom(typeof(IEnumerable)) && targetType.IsAssignableFrom(factoryAttribute.FactoredType) ) ) { foreach (TestCaseParameter parameter in CreateMultipleInstances(method)) { yield return(parameter); } } } }
private void GetAllDomains( IList <IDomain> domains, ParameterInfo parameter, object factory) { foreach (MethodInfo factoryMethod in ReflectionHelper.GetMethods(factory.GetType(), typeof(FactoryAttribute))) { if (factoryMethod.GetParameters().Length > 0) { continue; } Type returnType = factoryMethod.ReturnType; // check single object return if (parameter.ParameterType.IsAssignableFrom(returnType)) { object result = factoryMethod.Invoke(factory, null); IDomain domain = Domains.ToDomain(result); domain.Name = factoryMethod.Name; domains.Add(domain); continue; } // check array if (returnType.HasElementType) { Type elementType = returnType.GetElementType(); if (parameter.ParameterType == elementType) { object result = factoryMethod.Invoke(factory, null); IDomain domain = Domains.ToDomain(result); domain.Name = factoryMethod.Name; domains.Add(domain); continue; } } // check IEnumerable if (returnType.IsGenericType) { if (typeof(IEnumerable <>).IsAssignableFrom(returnType.GetGenericTypeDefinition())) { if (parameter.ParameterType.IsAssignableFrom(returnType.GetGenericArguments()[0])) { object result = factoryMethod.Invoke(factory, null); IDomain domain = Domains.ToDomain(result); domain.Name = factoryMethod.Name; domains.Add(domain); continue; } } } // check factory type FactoryAttribute factoryAttribute = ReflectionHelper.GetAttribute <FactoryAttribute>(factoryMethod); if (factoryAttribute != null) { Type factoredType = factoryAttribute.FactoredType; if (parameter.ParameterType == factoredType) { object result = factoryMethod.Invoke(factory, null); IDomain domain = Domains.ToDomain(result); domain.Name = factoryMethod.Name; domains.Add(domain); continue; } } } }