Exemplo n.º 1
0
        public void TestGeneric_FromBaseWithInterface()
        {
            ICustomAttribute[] attributes = AttributeUtility.GetCustomAttributes <ICustomAttribute> (BaseMemberWithSingleAttribute, true);

            Assert.That(attributes.Length, Is.EqualTo(1));
            Assert.That(attributes[0], Is.Not.Null);
        }
Exemplo n.º 2
0
        public void Test_FromProtectedOverrideWithAttribute()
        {
            InheritedAttribute[] attributes = AttributeUtility.GetCustomAttributes <InheritedAttribute> (DerivedProtectedMember, true);

            Assert.That(attributes.Length, Is.EqualTo(1));
            Assert.That(attributes[0], Is.Not.Null);
        }
 public void GetCustomAttributes_Unfiltered_Properties()
 {
     PerformMeasurement(
         _properties,
         items =>
     {
         var counter = 0;
         for (int i = 0; i < items.Length; ++i)
         {
             try
             {
                 counter += AttributeUtility.GetCustomAttributes(items[i], typeof(Attribute), true).Length;
             }
             catch (AmbiguousMatchException)
             {
             }
         }
         return(counter);
     },
         items =>
     {
         var counter = 0;
         for (int i = 0; i < items.Length; ++i)
         {
             try
             {
                 counter += Attribute.GetCustomAttributes(items[i], true).Length;
             }
             catch (AmbiguousMatchException)
             {
             }
         }
         return(counter);
     });
 }
 public void Filtering_WithDerivedAttributeType()
 {
     object[] attributes = AttributeUtility.GetCustomAttributes(typeof(BaseClassWithAttribute), typeof(DerivedInheritedAttribute), false);
     Assert.That(attributes, Is.EquivalentTo(new object[] {
         new DerivedInheritedAttribute("BaseClass")
     }));
 }
Exemplo n.º 5
0
        public void Test_ReturnsNewInstance()
        {
            var attribute1 = AttributeUtility.GetCustomAttributes(BaseMemberWithSingleAttribute, typeof(InheritedAttribute), false).Single();
            var attribute2 = AttributeUtility.GetCustomAttributes(BaseMemberWithSingleAttribute, typeof(InheritedAttribute), false).Single();

            Assert.That(attribute1, Is.Not.SameAs(attribute2));
        }
Exemplo n.º 6
0
        public void Test_FromOverrideHidingBaseAttribute()
        {
            var attributes = AttributeUtility.GetCustomAttributes <InheritedNotMultipleAttribute> (DerivedMemberHidingAttribute, true);

            Assert.That(attributes.Length, Is.EqualTo(1));
            Assert.That(attributes[0].Context, Is.EqualTo("Derived"));
        }
Exemplo n.º 7
0
        public void Test_ReturnsNewInstanceForType()
        {
            var attribute1 = AttributeUtility.GetCustomAttributes(typeof(SampleClass), typeof(InheritedAttribute), false).Single();
            var attribute2 = AttributeUtility.GetCustomAttributes(typeof(SampleClass), typeof(InheritedAttribute), false).Single();

            Assert.That(attribute1, Is.Not.SameAs(attribute2));
        }
Exemplo n.º 8
0
        public void Test_FromOverrideWithInterfaceAndMultiple()
        {
            ICustomAttribute[] attributes = AttributeUtility.GetCustomAttributes <ICustomAttribute> (DerivedMemberWithMultipleAttribute, true);

            Assert.That(attributes.Length, Is.EqualTo(2));
            Assert.That(attributes[0], Is.Not.Null);
            Assert.That(attributes[1], Is.Not.Null);
        }
Exemplo n.º 9
0
        public void Test_FromBaseWithInterface()
        {
            object[] attributes = AttributeUtility.GetCustomAttributes(BaseMemberWithSingleAttribute, typeof(ICustomAttribute), true);

            Assert.That(attributes.Length, Is.EqualTo(1));
            Assert.That(attributes[0], Is.Not.Null);
            Assert.IsInstanceOf(typeof(ICustomAttribute), attributes[0]);
        }
 public void DerivedClass_BaseAttributesAreSuppressed_InheritedTrue()
 {
     object[] attributes = AttributeUtility.GetCustomAttributes(typeof(DerivedWithAttributesAndSuppressed), typeof(Attribute), true);
     Assert.That(attributes, Is.EquivalentTo(new object[] {
         new BaseInheritedAttribute("DerivedWithAttributesAndSuppressed"),
         new DerivedInheritedAttribute("DerivedWithAttributesAndSuppressed")
     }));
 }
 public void DerivedDerivedClass_NothingSuppressed_InheritedFalse()
 {
     object[] attributes = AttributeUtility.GetCustomAttributes(typeof(DerivedDerivedWithAttributesForSuppressed), typeof(Attribute), false);
     Assert.That(attributes, Is.EquivalentTo(new object[] {
         new BaseInheritedAttribute("DerivedDerivedWithAttributesForSuppressed"),
         new DerivedInheritedAttribute("DerivedDerivedWithAttributesForSuppressed")
     }));
 }
 public void GetCustomAttributes_WithMemberInfo_DelegatesToTypeVersion()
 {
     object[] attributes = AttributeUtility.GetCustomAttributes((MemberInfo)typeof(DerivedWithAttributesAndSuppressed), typeof(Attribute), true);
     Assert.That(attributes, Is.EquivalentTo(new object[] {
         new BaseInheritedAttribute("DerivedWithAttributesAndSuppressed"),
         new DerivedInheritedAttribute("DerivedWithAttributesAndSuppressed")
     }));
 }
Exemplo n.º 13
0
        public void GetCustomAttributes()
        {
            var type    = typeof(DateTime);
            var adapter = TypeAdapter.Create(type);

            Assert.That(
                adapter.GetCustomAttributes <SerializableAttribute> (true),
                Is.EqualTo(AttributeUtility.GetCustomAttributes <SerializableAttribute> (type, true)));
        }
Exemplo n.º 14
0
 public void MixinAttribute_IsAlsoSuppressed()
 {
     using (MixinConfiguration.BuildNew().ForClass <DerivedWithAttributesAndSuppressed> ().AddMixin <MixinAddingInheritedAttribute> ().EnterScope())
     {
         Type type = TypeFactory.GetConcreteType(typeof(DerivedWithAttributesAndSuppressed));
         Assert.That(AttributeUtility.GetCustomAttributes(type, typeof(Attribute), true),
                     Has.No.Member(new BaseInheritedAttribute("MixinAddingInheritedAttribute")));
     }
 }
 public void Filtering_WithInterfaceType()
 {
     object[] attributes = AttributeUtility.GetCustomAttributes(typeof(BaseClassWithAttribute), typeof(ICustomAttribute), false);
     Assert.That(attributes, Is.EquivalentTo(new object[] {
         new BaseInheritedAttribute("BaseClass"),
         new DerivedInheritedAttribute("BaseClass"),
         new InheritedNotMultipleAttribute("BaseClass"),
     }));
 }
Exemplo n.º 16
0
        private Type GetStorageGroupType()
        {
            var storageGroupAttribute = AttributeUtility.GetCustomAttributes <StorageGroupAttribute> (Type, true).FirstOrDefault();

            if (storageGroupAttribute != null)
            {
                return(storageGroupAttribute.GetType());
            }
            return(null);
        }
Exemplo n.º 17
0
        public void MixinSuppressingNonInheritedAttributeOnTargetClass()
        {
            using (MixinConfiguration.BuildNew().ForClass <ClassWithNonInheritedAttribute> ().AddMixin <MixinSuppressingNonInheritedAttribute> ().EnterScope())
            {
                Type type = TypeFactory.GetConcreteType(typeof(ClassWithNonInheritedAttribute));

                Assert.That(AttributeUtility.GetCustomAttributes(type, typeof(Attribute), true),
                            Has.No.Member(new BaseNonInheritedAttribute("ClassWithNonInheritedAttribute")));
            }
        }
        public IResourceManager CreateResourceManager(Type type)
        {
            ArgumentUtility.CheckNotNull("type", type);

            var resourceAttributes = AttributeUtility.GetCustomAttributes <IResourcesAttribute> (type, false);
            var assembly           = type.Assembly;
            var resourceManagers   = resourceAttributes.Select(resourcesAttribute => GetResourceManagerFromCache(assembly, resourcesAttribute));

            return(ResourceManagerWrapper.CreateWrapperSet(resourceManagers));
        }
        public void Test_NoGetter()
        {
            var propertyInfo = typeof(DerivedSampleClass).GetProperty("PropertyWithoutGetter");

            object[] attributes = AttributeUtility.GetCustomAttributes(propertyInfo, typeof(InheritedAttribute), true);

            Assert.That(attributes.Length, Is.EqualTo(1));
            Assert.That(attributes[0], Is.Not.Null);
            Assert.IsInstanceOf(typeof(InheritedAttribute), attributes[0]);
        }
        public void Test_FromOverrideWithBaseOverloads()
        {
            var propertyInfo = typeof(DerivedSampleClass).GetProperty("Item", new[] { typeof(int) });

            object[] attributes = AttributeUtility.GetCustomAttributes(propertyInfo, typeof(InheritedAttribute), true);

            Assert.That(attributes.Length, Is.EqualTo(1));
            Assert.That(attributes[0], Is.Not.Null);
            Assert.IsInstanceOf(typeof(InheritedAttribute), attributes[0]);
        }
 public void DerivedClass_InheritedFalse()
 {
     object[] attributes = AttributeUtility.GetCustomAttributes(typeof(DerivedClassWithAttribute), typeof(Attribute), false);
     Assert.That(attributes, Is.EquivalentTo(new object[] {
         new BaseInheritedAttribute("DerivedClass"),
         new DerivedInheritedAttribute("DerivedClass"),
         new BaseNonInheritedAttribute("DerivedClass"),
         new DerivedNonInheritedAttribute("DerivedClass"),
         new InheritedNotMultipleAttribute("DerivedClass"),
     }));
 }
Exemplo n.º 22
0
        public void GetEnumerationValueFilter_FromAttributeProvider_IntegrationTestWithNullablePropertyType()
        {
            var provider = new EnumValueFilterProvider <DisableEnumValuesAttribute> (
                GetPropertyInformation("DisabledFromNullablePropertyType"),
                t => AttributeUtility.GetCustomAttributes <DisableEnumValuesAttribute> (t, true));

            var actual = provider.GetEnumerationValueFilter();

            Assert.That(actual, Is.TypeOf(typeof(ConstantEnumerationValueFilter)));
            Assert.That(((ConstantEnumerationValueFilter)actual).DisabledEnumValues, Is.EquivalentTo(new[] { TestEnum.Value5 }));
        }
Exemplo n.º 23
0
            protected override IEnumerable <MultiLingualNameAttribute> GetCustomAttributes(Enum value)
            {
                ArgumentUtility.CheckNotNull("value", value);

                var field = value.GetType().GetField(value.ToString(), BindingFlags.Static | BindingFlags.Public);

                if (field == null)
                {
                    return(Enumerable.Empty <MultiLingualNameAttribute>());
                }
                return(AttributeUtility.GetCustomAttributes <MultiLingualNameAttribute> (field, false));
            }
Exemplo n.º 24
0
        public void MixinSuppressingAttribute()
        {
            using (MixinConfiguration.BuildNew().ForClass <BaseWithAttributesForSuppressed> ().AddMixin <MixinAddingSuppressAttribute> ().EnterScope())
            {
                Type type = TypeFactory.GetConcreteType(typeof(BaseWithAttributesForSuppressed));
                Assert.That(type.GetCustomAttributes(true), Has.Member(new BaseInheritedAttribute("BaseWithAttributesForSuppressed")));
                Assert.That(type.GetCustomAttributes(true), Has.Member(new BaseInheritedAttribute("MixinAddingSuppressAttribute")));

                Assert.That(AttributeUtility.GetCustomAttributes(type, typeof(Attribute), true),
                            Has.No.Member(new BaseInheritedAttribute("BaseWithAttributesForSuppressed")));
                Assert.That(AttributeUtility.GetCustomAttributes(type, typeof(Attribute), true),
                            Has.Member(new BaseInheritedAttribute("MixinAddingSuppressAttribute")));
            }
        }
Exemplo n.º 25
0
        /// <inheritdoc />
        public object[] GetCustomAttributes(Type attributeType)
        {
            ArgumentUtility.CheckNotNull("attributeType", attributeType);

            var extensionTypes = (from info in GetValueInfos()
                                  select info.DefiningMethod.DeclaringType).Distinct();
            var attributes = from extensionType in extensionTypes
                             from attribute in AttributeUtility.GetCustomAttributes(extensionType, attributeType, false)
                             select attribute;
            var list = attributes.ToList();

            var array = (object[])Array.CreateInstance(attributeType, list.Count);

            list.CopyTo(array);
            return(array);
        }
Exemplo n.º 26
0
        /// <exception cref="InvalidOperationException">
        /// The enum type has an UndefinedEnumValueAttribute with a value that does not match the enum's type.
        /// <para>- or -</para>
        /// <para>The property is nullable and the property's type defines an UndefinedEnumValueAttribute</para>
        /// </exception>
        public EnumerationProperty(Parameters parameters)
            : base(parameters)
        {
            if (AttributeUtility.IsDefined <FlagsAttribute> (parameters.UnderlyingType, false))
            {
                throw new InvalidOperationException(
                          string.Format(
                              "The property '{0}' defined on type '{1}' is a flags-enum, which is not supported.",
                              Identifier,
                              PropertyInfo.DeclaringType));
            }
            _undefinedValue = GetUndefinedValue();

            var filterProvider = new EnumValueFilterProvider <DisableEnumValuesAttribute> (
                PropertyInfo,
                t => AttributeUtility.GetCustomAttributes <DisableEnumValuesAttribute> (t, true));

            _enumerationValueFilter = filterProvider.GetEnumerationValueFilter();
        }
        private XmlAttributeOverrides CreateAttributeOverrides(Type concreteType)
        {
            XmlAttributeOverrides attributeOverrides = new XmlAttributeOverrides();

            foreach (MemberInfo memberInfo in concreteType.FindMembers(
                         MemberTypes.Field | MemberTypes.Property,
                         BindingFlags.Instance | BindingFlags.Public,
                         delegate { return(true); },
                         null))
            {
                XmlAttributes attributes = new XmlAttributes();
                foreach (XmlElementAttribute attribute in AttributeUtility.GetCustomAttributes <XmlElementAttribute> (memberInfo, true))
                {
                    attributes.XmlElements.Add(attribute);
                }
                attributes.XmlAttribute = AttributeUtility.GetCustomAttribute <XmlAttributeAttribute> (memberInfo, true);
                attributes.XmlIgnore    = attributes.XmlAttribute == null && attributes.XmlElements.Count == 0;

                attributeOverrides.Add(concreteType, memberInfo.Name, attributes);
            }

            return(attributeOverrides);
        }
Exemplo n.º 28
0
 public void GetCustomAttributes_Filtered_Types()
 {
     PerformMeasurement(
         _types,
         items =>
     {
         var counter = 0;
         for (int i = 0; i < items.Length; ++i)
         {
             counter += AttributeUtility.GetCustomAttributes(items[i], typeof(SerializableAttribute), true).Length;
         }
         return(counter);
     },
         items =>
     {
         var counter = 0;
         for (int i = 0; i < items.Length; ++i)
         {
             counter += Attribute.GetCustomAttributes(items[i], typeof(SerializableAttribute), true).Length;
         }
         return(counter);
     });
 }
Exemplo n.º 29
0
 public void Test_FromBaseWithInvalidType()
 {
     AttributeUtility.GetCustomAttributes(BaseMemberWithSingleAttribute, typeof(object), true);
 }
Exemplo n.º 30
0
 public void TestGeneric_FromBaseWithInvalidType()
 {
     AttributeUtility.GetCustomAttributes <object> (BaseMemberWithSingleAttribute, true);
 }