Exemple #1
0
        private static ImportAttribute GetImportAttribute(ImportBuilder builder)
        {
            var list = new List <Attribute>();

            builder.BuildAttributes(typeof(FooImpl), ref list);
            Assert.AreEqual(1, list.Count);
            return(list[0] as ImportAttribute);
        }
        public void AllowRecomposition_SetsAllowRecompositionProperty()
        {
            var builder = new ImportBuilder();

            builder.AllowRecomposition();

            ImportAttribute importAtt = GetImportAttribute(builder);

            Assert.True(importAtt.AllowRecomposition);
            Assert.Null(importAtt.ContractType);
            Assert.Null(importAtt.ContractName);
        }
        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 AsContractTypeOfT_SetsContractType()
        {
            var builder = new ImportBuilder();

            builder.AsContractType <IFoo>();

            ImportAttribute importAtt = GetImportAttribute(builder);

            Assert.Equal(typeof(IFoo), importAtt.ContractType);
            Assert.Null(importAtt.ContractName);
            Assert.False(importAtt.AllowDefault);
            Assert.False(importAtt.AllowRecomposition);
        }
Exemple #6
0
        public void AllowDefault_SetsAllowDefaultProperty()
        {
            var builder = new ImportBuilder();

            builder.AllowDefault();

            ImportAttribute importAtt = GetImportAttribute(builder);

            Assert.IsTrue(importAtt.AllowDefault);
            Assert.IsFalse(importAtt.AllowRecomposition);
            Assert.IsNull(importAtt.ContractType);
            Assert.IsNull(importAtt.ContractName);
        }
        public void RequiredCreationPolicy_SetsRequiredCreationPolicyProperty()
        {
            var builder = new ImportBuilder();

            builder.RequiredCreationPolicy(CreationPolicy.NonShared);

            ImportAttribute importAtt = GetImportAttribute(builder);

            Assert.Equal(CreationPolicy.NonShared, importAtt.RequiredCreationPolicy);
            Assert.False(importAtt.AllowDefault);
            Assert.False(importAtt.AllowRecomposition);
            Assert.Null(importAtt.ContractType);
            Assert.Null(importAtt.ContractName);
        }
        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);
        }
Exemple #9
0
 public static void BuildAttributes(this ImportBuilder builder, Type type, ref List <Attribute> attributes)
 {
     builder.GetType()
     .GetMethod(nameof(BuildAttributes), BindingFlags.NonPublic | BindingFlags.Instance)
     .Invoke(builder, new object[] { type, attributes });
 }
Exemple #10
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;
        }