当前位置: 首页>>代码示例>>C#>>正文


C# CustomAttributeBuilder类代码示例

本文整理汇总了C#中CustomAttributeBuilder的典型用法代码示例。如果您正苦于以下问题:C# CustomAttributeBuilder类的具体用法?C# CustomAttributeBuilder怎么用?C# CustomAttributeBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


CustomAttributeBuilder类属于命名空间,在下文中一共展示了CustomAttributeBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetCustomAttribute_CustomAttributeBuilder

        public void SetCustomAttribute_CustomAttributeBuilder()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
            MethodBuilder method = type.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { typeof(int) });
            ParameterBuilder parameter = method.DefineParameter(1, ParameterAttributes.HasDefault, "testParam");
            ILGenerator ilGenerator = method.GetILGenerator();
            ilGenerator.Emit(OpCodes.Ret);

            Type attributeType = typeof(ParameterBuilderCustomAttribute);
            ConstructorInfo constructor = attributeType.GetConstructors()[0];
            FieldInfo field = attributeType.GetField("Field12345");


            CustomAttributeBuilder attribute = new CustomAttributeBuilder(constructor, new object[] { 4 }, new FieldInfo[] { field }, new object[] { "hello" });
            parameter.SetCustomAttribute(attribute);
            Type createdType = type.CreateTypeInfo().AsType();
            MethodInfo createdMethod = createdType.GetMethod("method1");
            ParameterInfo createdParameter = createdMethod.GetParameters()[0];

            object[] attributes = createdParameter.GetCustomAttributes(false).ToArray();
            Assert.Equal(1, attributes.Length);

            ParameterBuilderCustomAttribute obj = (ParameterBuilderCustomAttribute)attributes[0];
            Assert.Equal("hello", obj.Field12345);
            Assert.Equal(4, obj.m_ctorType2);
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:26,代码来源:SetCustomAttributeTests.cs

示例2: CanAddAttribute

    public void CanAddAttribute()
    {
        var type = typeof (SomeClass);

        AssemblyName aName = new AssemblyName("SomeNamespace");
        AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
        ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
        TypeBuilder tb = mb.DefineType(type.Name, TypeAttributes.Public, type);
        TypeDescriptor.AddAttributes(type);

        Type[] attrCtorParams = {typeof (string)};
        ConstructorInfo attrCtorInfo = typeof (DynamicAttribute).GetConstructor(attrCtorParams);

        if (attrCtorInfo != null)
        {
            CustomAttributeBuilder attrBuilder = new CustomAttributeBuilder(attrCtorInfo, new object[] {"Some Value"});
            tb.SetCustomAttribute(attrBuilder);
        }

        Type newType = tb.CreateType();
        SomeClass instance = (SomeClass) Activator.CreateInstance(newType);
        DynamicAttribute attr = (DynamicAttribute) instance.GetType().GetCustomAttributes(typeof (DynamicAttribute), false).SingleOrDefault();

        Assert.AreEqual("Test", instance.Value);
        Assert.IsNotNull(attr);
        Assert.AreEqual(attr.Value, "Some Value");
    }
开发者ID:Foxpips,项目名称:ProgrammingCSharp,代码行数:27,代码来源:DynamicAttribute.cs

示例3: SetCustomAttribute

		public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
		{
			Universe u = this.Module.universe;
			if (customBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_FieldOffsetAttribute)
			{
				customBuilder = customBuilder.DecodeBlob(this.Module.Assembly);
				SetOffset((int)customBuilder.GetConstructorArgument(0));
			}
			else if (customBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_MarshalAsAttribute)
			{
				MarshalSpec.SetMarshalAsAttribute(typeBuilder.ModuleBuilder, pseudoToken, customBuilder);
				attribs |= FieldAttributes.HasFieldMarshal;
			}
			else if (customBuilder.Constructor.DeclaringType == u.System_NonSerializedAttribute)
			{
				attribs |= FieldAttributes.NotSerialized;
			}
			else if (customBuilder.Constructor.DeclaringType == u.System_Runtime_CompilerServices_SpecialNameAttribute)
			{
				attribs |= FieldAttributes.SpecialName;
			}
			else
			{
				typeBuilder.ModuleBuilder.SetCustomAttribute(pseudoToken, customBuilder);
			}
		}
开发者ID:koush,项目名称:mono,代码行数:26,代码来源:FieldBuilder.cs

示例4: SetCustomAttribute

        public void SetCustomAttribute()
        {
            Type returnType = typeof(int);
            Type[] ctorParamTypes = new Type[] { typeof(int) };

            int expectedValue = 10;
            object[] ctorParamValues = new object[] { expectedValue };
            CustomAttributeBuilder customAttrBuilder = new CustomAttributeBuilder(typeof(IntPropertyAttribute).GetConstructor(ctorParamTypes), ctorParamValues);
            
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);

            PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, returnType, null);
            property.SetCustomAttribute(customAttrBuilder);

            MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
            MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, returnType, new Type[0]);

            ILGenerator methodILGenerator = method.GetILGenerator();
            methodILGenerator.Emit(OpCodes.Ldarg_0);
            methodILGenerator.Emit(OpCodes.Ret);
            
            property.SetGetMethod(method);

            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static;
            Type createdType = type.CreateTypeInfo().AsType();
            PropertyInfo createdProperty = createdType.GetProperty("TestProperty", bindingFlags);
            object[] attributes = createdProperty.GetCustomAttributes(false).ToArray();

            Assert.Equal(1, attributes.Length);
            Assert.True(attributes[0] is IntPropertyAttribute);
            Assert.Equal(expectedValue, (attributes[0] as IntPropertyAttribute).Value);
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:32,代码来源:PropertyBuilderSetCustomAttribute.cs

示例5: SetCustomAttribute_CustomAttributeBuilder

        public void SetCustomAttribute_CustomAttributeBuilder()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
            FieldBuilder field = type.DefineField("TestField", typeof(object), FieldAttributes.Public);
            ConstructorInfo attributeConstructor = typeof(EmptyAttribute).GetConstructor(new Type[0]);
            CustomAttributeBuilder attribute = new CustomAttributeBuilder(attributeConstructor, new object[0]);

            field.SetCustomAttribute(attribute);
        }
开发者ID:AndreGleichner,项目名称:corefx,代码行数:9,代码来源:FieldBuilderSetCustomAttribute.cs

示例6: SetCustomAttribute_CustomAttributeBuilder

        public void SetCustomAttribute_CustomAttributeBuilder()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
            EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
            ConstructorInfo attributeConstructor = typeof(EmptyAttribute).GetConstructor(new Type[0]);
            CustomAttributeBuilder attribute = new CustomAttributeBuilder(attributeConstructor, new object[0]);

            eventBuilder.SetCustomAttribute(attribute);
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:9,代码来源:EventBuilderSetCustomAttribute.cs

示例7: SetCustomAttribute_CustomAttributeBuilder_TypeCreated_ThrowsInvalidOperationException

        public void SetCustomAttribute_CustomAttributeBuilder_TypeCreated_ThrowsInvalidOperationException()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
            FieldBuilder field = type.DefineField("TestField", typeof(object), FieldAttributes.Public);
            ConstructorInfo con = typeof(EmptyAttribute).GetConstructor(new Type[0]);
            CustomAttributeBuilder attribute = new CustomAttributeBuilder(con, new object[0]);
            type.CreateTypeInfo().AsType();

            Assert.Throws<InvalidOperationException>(() => { field.SetCustomAttribute(attribute); });
        }
开发者ID:AndreGleichner,项目名称:corefx,代码行数:10,代码来源:FieldBuilderSetCustomAttribute.cs

示例8: SetCustomAttribute_CustomAttributeBuilder

        public void SetCustomAttribute_CustomAttributeBuilder()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
            string[] typeParamNames = new string[] { "TFirst" };
            GenericTypeParameterBuilder[] typeParams = type.DefineGenericParameters(typeParamNames);
            ConstructorInfo constructorinfo = typeof(HelperAttribute).GetConstructor(new Type[] { typeof(string) });
            CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(constructorinfo, new object[] { "TestString" });

            typeParams[0].SetCustomAttribute(attributeBuilder);
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:10,代码来源:GenericTypeParameterBuilderSetCustomAttribute.cs

示例9: SetCustomAttribute_CustomAttributeBuilder_TypeCreated_ThrowsInvalidOperationException

        public void SetCustomAttribute_CustomAttributeBuilder_TypeCreated_ThrowsInvalidOperationException()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
            EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
            ConstructorInfo attributeConstructor = typeof(EmptyAttribute).GetConstructor(new Type[0]);
            CustomAttributeBuilder attribute = new CustomAttributeBuilder(attributeConstructor, new object[0]);
            type.CreateTypeInfo().AsType();

            Assert.Throws<InvalidOperationException>(() => eventBuilder.SetCustomAttribute(attribute));
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:10,代码来源:EventBuilderSetCustomAttribute.cs

示例10: SetCustomAttribute_CustomAttributeBuilder

        public void SetCustomAttribute_CustomAttributeBuilder()
        {
            ModuleBuilder module = Helpers.DynamicModule();
            ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
            CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { 5 });
            module.SetCustomAttribute(attributeBuilder);

            object[] attributes = module.GetCustomAttributes().ToArray();
            Assert.Equal(1, attributes.Length);
            Assert.True(attributes[0] is IntAllAttribute);
            Assert.Equal(5, ((IntAllAttribute)attributes[0])._i);
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:12,代码来源:ModuleBuilderSetCustomAttribute.cs

示例11: SetCustomAttribute_CustomAttributeBuilder_TypeNotCreated_ThrowsInvalidOperationException

        public void SetCustomAttribute_CustomAttributeBuilder_TypeNotCreated_ThrowsInvalidOperationException()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
            PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(int), null);

            Type[] ctorParamTypes = new Type[] { typeof(int) };
            object[] ctorParamValues = new object[] { 10 };
            CustomAttributeBuilder customAttrBuilder = new CustomAttributeBuilder(typeof(IntPropertyAttribute).GetConstructor(ctorParamTypes), ctorParamValues);

            type.CreateTypeInfo().AsType();
            Assert.Throws<InvalidOperationException>(() => property.SetCustomAttribute(customAttrBuilder));
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:12,代码来源:PropertyBuilderSetCustomAttribute.cs

示例12: SetCustomAttribute

        public void SetCustomAttribute()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
            ConstructorInfo constructor = typeof(TypeBuilderStringAttribute).GetConstructor(new Type[] { typeof(string) });
            CustomAttributeBuilder cuatbu = new CustomAttributeBuilder(constructor, new object[] { "hello" });
            type.SetCustomAttribute(cuatbu);

            type.CreateTypeInfo().AsType();
            
            object[] attributes = type.GetCustomAttributes(false).ToArray();
            Assert.Equal(1, attributes.Length);
            Assert.True(attributes[0] is TypeBuilderStringAttribute);
            Assert.Equal("hello", ((TypeBuilderStringAttribute)attributes[0]).Creator);
        }
开发者ID:Corillian,项目名称:corefx,代码行数:14,代码来源:TypeBuilderSetCustomAttribute.cs

示例13: SetCustomAttribute

		public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
		{
			Universe u = typeBuilder.ModuleBuilder.universe;
			if (customBuilder.Constructor.DeclaringType == u.System_Runtime_CompilerServices_SpecialNameAttribute)
			{
				attributes |= EventAttributes.SpecialName;
			}
			else
			{
				if (lazyPseudoToken == 0)
				{
					lazyPseudoToken = typeBuilder.ModuleBuilder.AllocPseudoToken();
				}
				typeBuilder.ModuleBuilder.SetCustomAttribute(lazyPseudoToken, customBuilder);
			}
		}
开发者ID:koush,项目名称:mono,代码行数:16,代码来源:EventBuilder.cs

示例14: SetCustomAttribute_CustomAttributeBuilder

        public void SetCustomAttribute_CustomAttributeBuilder()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);

            Type attributeType = typeof(TypeBuilderIntAttribute);
            ConstructorInfo attriubteConstructor = attributeType.GetConstructors()[0];
            FieldInfo attributeField = attributeType.GetField("Field12345");

            CustomAttributeBuilder attribute = new CustomAttributeBuilder(attriubteConstructor, new object[] { 4 }, new FieldInfo[] { attributeField }, new object[] { "hello" });
            type.SetCustomAttribute(attribute);
            type.CreateTypeInfo().AsType();
            
            object[] attributes = type.GetCustomAttributes(false).ToArray();
            Assert.Equal(1, attributes.Length);

            TypeBuilderIntAttribute obj = (TypeBuilderIntAttribute)attributes[0];
            Assert.Equal("hello", obj.Field12345);
            Assert.Equal(4, obj.m_ctorType2);
        }
开发者ID:Corillian,项目名称:corefx,代码行数:19,代码来源:TypeBuilderSetCustomAttribute.cs

示例15: SetCustomAttribute_CustomAttributeBuilder

        public void SetCustomAttribute_CustomAttributeBuilder()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
            ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
            ILGenerator ilGenerator = constructor.GetILGenerator();
            ilGenerator.Emit(OpCodes.Ldarg_1);

            ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
            CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { 2 });

            constructor.SetCustomAttribute(attributeBuilder);
            Type createdType = type.CreateTypeInfo().AsType();

            ConstructorInfo createdConstructor = createdType.GetConstructor(new Type[0]);
            Attribute[] customAttributes = createdConstructor.GetCustomAttributes(true).ToArray();

            Assert.Equal(1, customAttributes.Length);
            Assert.Equal(2, ((IntAllAttribute)customAttributes[0])._i);
        }
开发者ID:swaroop-sridhar,项目名称:corefx,代码行数:19,代码来源:ConstructorBuilderSetCustomAttribute.cs


注:本文中的CustomAttributeBuilder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。