public void AsContractName_AndContractType_SetsContractNameAndType()
        {
            var builder = new ImportBuilder();

            builder.AsContractName("hey");
            builder.AsContractType(typeof(IFoo));

            ImportAttribute importAtt = GetImportAttribute(builder);

            Assert.Equal("hey", importAtt.ContractName);
            Assert.Equal(typeof(IFoo), importAtt.ContractType);
        }
        public void AsContractName_SetsContractName()
        {
            var builder = new ImportBuilder();

            builder.AsContractName("hey");

            ImportAttribute importAtt = GetImportAttribute(builder);

            Assert.Equal("hey", importAtt.ContractName);
            Assert.False(importAtt.AllowDefault);
            Assert.False(importAtt.AllowRecomposition);
            Assert.Null(importAtt.ContractType);
        }
        public void AsMany_And_ContractName_ChangesGeneratedAttributeToImportMany()
        {
            var builder = new ImportBuilder();

            builder.AsContractName("hey");
            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.Equal("hey", att.ContractName);
            Assert.Null(att.ContractType);
        }
Beispiel #4
0
        /// <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;
        }