Exemplo n.º 1
0
 private static Attribute[] GetAttributesFromMember(ConventionBuilder builder, Type type, string member)
 {
     if (string.IsNullOrEmpty(member))
     {
         Attribute[] list = builder.GetDeclaredAttributes(null, type.GetTypeInfo());
         return(list);
     }
     else
     {
         PropertyInfo pi   = type.GetRuntimeProperty(member);
         Attribute[]  list = builder.GetDeclaredAttributes(type, pi);
         return(list);
     }
 }
Exemplo n.º 2
0
        private static ImportMetadataConstraintAttribute GetImportMetadataConstraintAttribute(ConventionBuilder builder)
        {
            var list = builder.GetDeclaredAttributes(typeof(FooImpl), typeof(FooImpl).GetRuntimeProperties().Where((m) => m.Name == "IFooProperty").First());

            Assert.Equal(2, list.Length);
            return(list.OfType <ImportMetadataConstraintAttribute>().FirstOrDefault());
        }
Exemplo n.º 3
0
        private static Attribute GetAttributeFromMember(ConventionBuilder builder, Type type, string member)
        {
            PropertyInfo pi = type.GetRuntimeProperty(member);

            Attribute[] list = builder.GetDeclaredAttributes(type, pi);
            return(list[0] as Attribute);
        }
Exemplo n.º 4
0
        public void ExportInterfaceWithTypeOf2()
        {
            var builder = new ConventionBuilder();
            builder.ForType(typeof(CFoo)).Export((c) => c.AsContractType(typeof(IFoo)));

            var exports = builder.GetDeclaredAttributes(typeof(CFoo), typeof(CFoo).GetTypeInfo()).Where<Attribute>(e => e is ExportAttribute).Cast<ExportAttribute>();
            Assert.Equal(1, exports.Count());
            Assert.Equal(exports.First().ContractType, typeof(IFoo));
        }
Exemplo n.º 5
0
        public void ExportInterfaceWithTypeOf2()
        {
            var builder = new ConventionBuilder();

            builder.ForType(typeof(FooImpl)).Export((c) => c.AsContractType(typeof(IFoo)));

            Collections.Generic.IEnumerable <ExportAttribute> exports = builder.GetDeclaredAttributes(typeof(FooImpl), typeof(FooImpl).GetTypeInfo()).Where <Attribute>(e => e is ExportAttribute).Cast <ExportAttribute>();
            Assert.Equal(1, exports.Count());
            Assert.Equal(exports.First().ContractType, typeof(IFoo));
        }
Exemplo n.º 6
0
        public void ExportInterfaceWithTypeOf1()
        {
            var builder = new ConventionBuilder();

            builder.ForType <CFoo>().Export <IFoo>();

            IEnumerable <ExportAttribute> exports = builder.GetDeclaredAttributes(typeof(CFoo), typeof(CFoo).GetTypeInfo()).Where <Attribute>(e => e is ExportAttribute).Cast <ExportAttribute>();

            Assert.Equal(1, exports.Count());
            Assert.Equal(exports.First().ContractType, typeof(IFoo));
        }
Exemplo n.º 7
0
        public void MapType_ShouldReturnProjectedAttributesForType()
        {
            var builder = new ConventionBuilder();

            builder.
            ForTypesDerivedFrom <IFoo>().
            Export <IFoo>();

            var fooImplAttributes = builder.GetDeclaredAttributes(typeof(FooImpl), typeof(FooImpl).GetTypeInfo());
            var fooImplWithConstructorsAttributes = builder.GetDeclaredAttributes(typeof(FooImplWithConstructors), typeof(FooImplWithConstructors).GetTypeInfo());

            var exports = new List <object>();

            exports.AddRange(fooImplAttributes);
            exports.AddRange(fooImplWithConstructorsAttributes);
            Assert.Equal(2, exports.Count);

            foreach (var exportAttribute in exports)
            {
                Assert.Equal(typeof(IFoo), ((ExportAttribute)exportAttribute).ContractType);
                Assert.Null(((ExportAttribute)exportAttribute).ContractName);
            }
        }
Exemplo n.º 8
0
        public void MapType_ShouldReturnProjectedAttributesForType()
        {
            var builder = new ConventionBuilder();

            builder.
                ForTypesDerivedFrom<IFoo>().
                Export<IFoo>();

            var fooImplAttributes = builder.GetDeclaredAttributes(typeof(FooImpl), typeof(FooImpl).GetTypeInfo());
            var fooImplWithConstructorsAttributes = builder.GetDeclaredAttributes(typeof(FooImplWithConstructors), typeof(FooImplWithConstructors).GetTypeInfo());

            var exports = new List<object>();

            exports.AddRange(fooImplAttributes);
            exports.AddRange(fooImplWithConstructorsAttributes);
            Assert.Equal(2, exports.Count);

            foreach (var exportAttribute in exports)
            {
                Assert.Equal(typeof(IFoo), ((ExportAttribute)exportAttribute).ContractType);
                Assert.Null(((ExportAttribute)exportAttribute).ContractName);
            }
        }
Exemplo n.º 9
0
        private static ConstructorInfo GetSelectedConstructor(ConventionBuilder builder, Type type)
        {
            ConstructorInfo reply = null;

            foreach (ConstructorInfo ci in type.GetTypeInfo().DeclaredConstructors)
            {
                Attribute[] li = builder.GetDeclaredAttributes(type, ci);
                if (li.Length > 0)
                {
                    Assert.True(reply == null);                   // Fail if we got more than one constructor
                    reply = ci;
                }
            }

            return(reply);
        }
Exemplo n.º 10
0
        public void ManuallySelectingConstructor_SelectsTheExplicitOne_IEnumerableParameterBecomesImportMany()
        {
            var builder = new ConventionBuilder();

            builder.ForType(typeof(FooImplWithConstructors)).SelectConstructor(cis => cis.ElementAt(2));

            ConstructorInfo selectedConstructor = GetSelectedConstructor(builder, typeof(FooImplWithConstructors));

            Assert.NotNull(selectedConstructor);
            Assert.Equal(1, selectedConstructor.GetParameters().Length);     // Should select public FooImplWithConstructors(IEnumerable<IFoo>) { }

            ParameterInfo pi = selectedConstructor.GetParameters()[0];

            Assert.Equal(typeof(IEnumerable <IFoo>), pi.ParameterType);

            Attribute[] attributes = builder.GetDeclaredAttributes(typeof(FooImplWithConstructors), pi);
            Assert.Equal(1, attributes.Count());
            Assert.NotNull(attributes[0] as ImportManyAttribute);

            attributes = GetAttributesFromMember(builder, typeof(FooImplWithConstructors), null);
            Assert.Equal(0, attributes.Count());
        }
Exemplo n.º 11
0
 private static IEnumerable <ExportAttribute> GetExportAttributes(ConventionBuilder builder, Type type)
 {
     Attribute[] list = builder.GetDeclaredAttributes(type, type.GetTypeInfo());
     return(list.Cast <ExportAttribute>());
 }
Exemplo n.º 12
0
 private static ExportMetadataAttribute GetExportMetadataAttribute(ConventionBuilder builder)
 {
     Attribute[] list = builder.GetDeclaredAttributes(typeof(FooImpl), typeof(FooImpl).GetTypeInfo());
     Assert.Equal(2, list.Length);
     return(list[1] as ExportMetadataAttribute);
 }
 private static IEnumerable<ExportAttribute> GetExportAttributes(ConventionBuilder builder, Type type)
 {
     var list = builder.GetDeclaredAttributes(type, type.GetTypeInfo());
     return list.Cast<ExportAttribute>();
 }
Exemplo n.º 14
0
 private static ImportMetadataConstraintAttribute GetImportMetadataConstraintAttribute(ConventionBuilder builder)
 {
     var list = builder.GetDeclaredAttributes(typeof(FooImpl), typeof(FooImpl).GetRuntimeProperties().Where((m) => m.Name == "IFooProperty").First());
     Assert.Equal(2, list.Length);
     return list.OfType<ImportMetadataConstraintAttribute>().FirstOrDefault();
 }
Exemplo n.º 15
0
        public void ManuallySelectingConstructor_SelectsTheExplicitOne()
        {
            var builder = new ConventionBuilder();
            builder.ForType(typeof(FooImplWithConstructors)).SelectConstructor(cis => cis.ElementAt(1));

            var selectedConstructor = GetSelectedConstructor(builder, typeof(FooImplWithConstructors));
            Assert.NotNull(selectedConstructor);
            Assert.Equal(1, selectedConstructor.GetParameters().Length);     // Should select public FooImplWithConstructors(int) { }

            var pi = selectedConstructor.GetParameters()[0];
            Assert.Equal(typeof(int), pi.ParameterType);

            var attributes = builder.GetDeclaredAttributes(typeof(FooImplWithConstructors), pi);
            Assert.Equal(1, attributes.Count());
            Assert.NotNull(attributes[0] as ImportAttribute);

            attributes = GetAttributesFromMember(builder, typeof(FooImplWithConstructors), null);
            Assert.Equal(0, attributes.Count());
        }
 private static Attribute GetAttributeFromMember(ConventionBuilder builder, Type type, string member)
 {
     var pi = type.GetRuntimeProperty(member);
     var list = builder.GetDeclaredAttributes(type, pi);
     return list[0] as Attribute;
 }
Exemplo n.º 17
0
 private static ExportMetadataAttribute GetExportMetadataAttribute(ConventionBuilder builder)
 {
     var list = builder.GetDeclaredAttributes(typeof(FooImpl), typeof(FooImpl).GetTypeInfo());
     Assert.Equal(2, list.Length);
     return list[1] as ExportMetadataAttribute;
 }
Exemplo n.º 18
0
        private static ConstructorInfo GetSelectedConstructor(ConventionBuilder builder, Type type)
        {
            ConstructorInfo reply = null;
            foreach (var ci in type.GetTypeInfo().DeclaredConstructors)
            {
                var li = builder.GetDeclaredAttributes(type, ci);
                if (li.Length > 0)
                {
                    Assert.True(reply == null);                   // Fail if we got more than one constructor
                    reply = ci;
                }
            }

            return reply;
        }
Exemplo n.º 19
0
 private static Attribute[] GetAttributesFromMember(ConventionBuilder builder, Type type, string member)
 {
     if (string.IsNullOrEmpty(member))
     {
         var list = builder.GetDeclaredAttributes(null, type.GetTypeInfo());
         return list;
     }
     else
     {
         var pi = type.GetRuntimeProperty(member);
         var list = builder.GetDeclaredAttributes(type, pi);
         return list;
     }
 }
Exemplo n.º 20
0
        public void ManuallySelectingConstructor_SelectsTheExplicitOne_IEnumerableParameterBecomesImportMany()
        {
            var builder = new ConventionBuilder();
            builder.ForType<FooImplWithConstructors>().SelectConstructor(param => new FooImplWithConstructors(param.Import<IEnumerable<IFoo>>()));

            var selectedConstructor = GetSelectedConstructor(builder, typeof(FooImplWithConstructors));
            Assert.NotNull(selectedConstructor);
            Assert.Equal(1, selectedConstructor.GetParameters().Length);     // Should select public FooImplWithConstructors(IEnumerable<IFoo>) { }

            var pi = selectedConstructor.GetParameters()[0];
            Assert.Equal(typeof(IEnumerable<IFoo>), pi.ParameterType);

            var attributes = builder.GetDeclaredAttributes(typeof(FooImplWithConstructors), pi);
            Assert.Equal(1, attributes.Count());
            Assert.NotNull(attributes[0] as ImportManyAttribute);

            attributes = GetAttributesFromMember(builder, typeof(FooImplWithConstructors), null);
            Assert.Equal(0, attributes.Count());
        }