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.CreateType(); 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); }
public void SetCustomAttribute_ConstructorInfo_ByteArray() { 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); parameter.SetCustomAttribute(typeof(ParameterBuilderCustomAttribute).GetConstructor(new Type[] { typeof(bool) }), new byte[] { 1, 0, 1, 0, 0 }); Type createdType = type.CreateType(); MethodInfo createdMethod = createdType.GetMethod("method1"); ParameterInfo createdParameter = createdMethod.GetParameters()[0]; object[] attributes = createdParameter.GetCustomAttributes(false).Select(a => (object)a).ToArray(); Assert.Equal(1, attributes.Length); ParameterBuilderCustomAttribute obj = (ParameterBuilderCustomAttribute)attributes[0]; Assert.True(obj.booleanValue); }