Exemplo n.º 1
0
        [Test] // GetCustomAttributes (Type, Boolean)
        public void GetCustomAttributes2()
        {
            ConstructorBuilder cb = genClass.DefineConstructor(
                0, 0, new Type [1] {
                typeof(int)
            });

            cb.GetILGenerator().Emit(OpCodes.Ret);

            try {
                cb.GetCustomAttributes(null, true);
                Assert.Fail("#A1");
            } catch (NotSupportedException ex) {
                // The invoked member is not supported in a dynamic
                // module
                Assert.AreEqual(typeof(NotSupportedException), ex.GetType(), "#A2");
                Assert.IsNull(ex.InnerException, "#A3");
                Assert.IsNotNull(ex.Message, "#A4");
            }

            genClass.CreateType();

            try {
                cb.GetCustomAttributes(null, true);
                Assert.Fail("#B1");
            } catch (NotSupportedException ex) {
                // The invoked member is not supported in a dynamic
                // module
                Assert.AreEqual(typeof(NotSupportedException), ex.GetType(), "#B2");
                Assert.IsNull(ex.InnerException, "#B3");
                Assert.IsNotNull(ex.Message, "#B4");
            }
        }
Exemplo n.º 2
0
        [Test] // GetCustomAttributes (Type, Boolean)
        public void GetCustomAttributes2_Incomplete()
        {
            ConstructorBuilder cb = genClass.DefineConstructor(
                MethodAttributes.Public, 0,
                new Type [1] {
                typeof(int)
            });

            cb.GetILGenerator().Emit(OpCodes.Ret);

            Type            attrType = typeof(ObsoleteAttribute);
            ConstructorInfo ctorInfo =
                attrType.GetConstructor(new Type [] { typeof(String) });

            cb.SetCustomAttribute(new CustomAttributeBuilder(ctorInfo, new object [] { "FOO" }));

            try {
                cb.GetCustomAttributes(attrType, false);
                Assert.Fail("#1");
            } catch (NotSupportedException ex) {
                // The invoked member is not supported in a dynamic
                // module
                Assert.AreEqual(typeof(NotSupportedException), ex.GetType(), "#2");
                Assert.IsNull(ex.InnerException, "#3");
                Assert.IsNotNull(ex.Message, "#4");
            }
        }
        public void GetCustomAttributes_ThrowsNotSupportedException()
        {
            TypeBuilder        type        = Helpers.DynamicType(TypeAttributes.Public);
            ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);

            Assert.Throws <NotSupportedException>(() => constructor.GetCustomAttributes());
        }