public void IsDefined()
        {
            Assembly thisAsm = typeof(GetCustomAttributes_Assembly).GetTypeInfo().Assembly;

            Assert.True(CustomAttributeExtensions.IsDefined(thisAsm, typeof(MyAttribute_AllowMultiple)));

            Assert.False(CustomAttributeExtensions.IsDefined(thisAsm, typeof(SecurityCriticalAttribute)));
        }
        public void CustomAttributes_Get()
        {
            Assembly thisAsm = typeof(GetCustomAttributes_Assembly).GetTypeInfo().Assembly;
            IEnumerable <CustomAttributeData> attributeData = thisAsm.CustomAttributes;
            IEnumerator <CustomAttributeData> customAttrs   = attributeData.GetEnumerator();

            Assert.Equal(1, attributeData.Count(attr => attr.ToString().Contains("MyAttribute_Single")));
            Assert.Equal(2, attributeData.Count(attr => attr.ToString().Contains("MyAttribute_AllowMultiple")));
        }
        public void GetCustomAttributes_thisAsm()
        {
            Assembly thisAsm = typeof(GetCustomAttributes_Assembly).GetTypeInfo().Assembly;
            IEnumerable <Attribute> attributes = CustomAttributeExtensions.GetCustomAttributes(thisAsm);

            //There are other attributes added as default:

            Assert.Equal(1, attributes.Count(attr => attr.ToString().Equals("System.Reflection.Tests.MyAttribute_Single single", StringComparison.Ordinal)));
            Assert.Equal(1, attributes.Count(attr => attr.ToString().Equals("System.Reflection.Tests.MyAttribute_AllowMultiple multiple1", StringComparison.Ordinal)));
            Assert.Equal(1, attributes.Count(attr => attr.ToString().Equals("System.Reflection.Tests.MyAttribute_AllowMultiple multiple2", StringComparison.Ordinal)));
        }
        public void GetCustomAttributesOfT()
        {
            Assembly thisAsm = typeof(GetCustomAttributes_Assembly).GetTypeInfo().Assembly;
            IEnumerable <Attribute> attributes;

            attributes = CustomAttributeExtensions.GetCustomAttributes <MyAttribute_AllowMultiple>(thisAsm);
            Assert.Equal(2, attributes.Count());
            Assert.Equal(1, attributes.Count(attr => attr.ToString().Equals("System.Reflection.Tests.MyAttribute_AllowMultiple multiple1", StringComparison.Ordinal)));
            Assert.Equal(1, attributes.Count(attr => attr.ToString().Equals("System.Reflection.Tests.MyAttribute_AllowMultiple multiple2", StringComparison.Ordinal)));

            attributes = CustomAttributeExtensions.GetCustomAttributes <SecurityCriticalAttribute>(thisAsm);
            Assert.Equal(0, attributes.Count());
        }
        public void GetCustomAttributeExtension()
        {
            Assembly  thisAsm   = typeof(GetCustomAttributes_Assembly).GetTypeInfo().Assembly;
            Attribute attribute = CustomAttributeExtensions.GetCustomAttribute(thisAsm, typeof(SecurityCriticalAttribute));

            Assert.Null(attribute);

            attribute = CustomAttributeExtensions.GetCustomAttribute(thisAsm, typeof(MyAttribute_Single));
            Assert.Equal("System.Reflection.Tests.MyAttribute_Single single", attribute.ToString());

            Assert.Throws <AmbiguousMatchException>(() =>
            {
                attribute = CustomAttributeExtensions.GetCustomAttribute(thisAsm, typeof(MyAttribute_AllowMultiple));
            });
        }
        public void GetCustomAttributeOfT()
        {
            Assembly  thisAsm = typeof(GetCustomAttributes_Assembly).GetTypeInfo().Assembly;
            Attribute attribute;

            attribute = CustomAttributeExtensions.GetCustomAttribute <SecurityCriticalAttribute>(thisAsm);
            Assert.Null(attribute);

            AssertExtensions.Throws <ArgumentException>(null, () =>
            {
                CustomAttributeExtensions.GetCustomAttributes(thisAsm, typeof(string));
            });

            attribute = CustomAttributeExtensions.GetCustomAttribute <MyAttribute_Single>(thisAsm);
            Assert.Equal("System.Reflection.Tests.MyAttribute_Single single", attribute.ToString());

            Assert.Throws <AmbiguousMatchException>(() =>
            {
                attribute = CustomAttributeExtensions.GetCustomAttribute <Attribute>(thisAsm);
            });
        }