public void AsMany_ChangesGeneratedAttributeToImportMany() { var builder = new ImportBuilder(); builder.AsMany(); var list = new List <Attribute>(); builder.BuildAttributes(typeof(FooImpl), ref list); Assert.Equal(1, list.Count); var att = list[0] as ImportManyAttribute; Assert.NotNull(att); Assert.Null(att.ContractName); Assert.Null(att.ContractType); }
/// <summary> /// Builds the given parameter. /// </summary> /// <param name="parameter"></param> /// <param name="builder"></param> void BuildParameter(ParameterInfo parameter, ImportBuilder builder) { Debug.Assert(parameter != null); Debug.Assert(builder != null); var name = parameter.ParameterType.FullName; { // decides whether the parameter is attempting to import many items Type importManyType = null; if (parameter.ParameterType.IsGenericType && parameter.ParameterType.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { importManyType = parameter.ParameterType.GetGenericArguments()[0]; } if (parameter.ParameterType.IsGenericType && parameter.ParameterType.GetGenericTypeDefinition() == typeof(ICollection <>)) { importManyType = parameter.ParameterType.GetGenericArguments()[0]; } if (parameter.ParameterType.IsArray) { importManyType = parameter.ParameterType.GetElementType(); } if (importManyType != null) { builder.AsContractType(importManyType); builder.AsMany(true); builder.AllowDefault(); return; } } { // decides whether the parameter is attempting to import a Func<T>; essentially an ExportFactory Type funcType = null; if (parameter.ParameterType.IsGenericType && parameter.ParameterType.GetGenericArguments().Length == 1 && parameter.ParameterType.GetGenericTypeDefinition() == typeof(Func <>)) { funcType = parameter.ParameterType.GetGenericArguments()[0]; } if (funcType != null) { // import a Func generated by our FuncFactory var contractType = typeof(Func <>).MakeGenericType(funcType); var contractName = "FuncFactory:" + AttributedModelServices.GetContractName(typeof(Func <>)); builder.AsContractType(contractType); builder.AsContractName(contractName); builder.AsMany(false); builder.AllowDefault(); return; } } // fall back to normal method builder.AsContractType(parameter.ParameterType); builder.AsMany(false); return; }