private static void CopyNonInheritableAttributes(GenericTypeParameterBuilder newGenericParameter, Type originalGenericArgument) { foreach (var attribute in AttributeUtil.GetNonInheritableAttributes(originalGenericArgument)) { newGenericParameter.SetCustomAttribute(attribute); } }
/// <summary> /// Applies an attribute to a generic type parameter. /// </summary> /// <param name="parameter">An instance of GenericTypeParameterBuilder to apply the attribute to.</param> /// <param name="expression">An expression that represents the attribute.</param> public static void SetCustomAttribute(this GenericTypeParameterBuilder parameter, Expression <Func <Attribute> > expression) { var builder = parameter.Module.Assembly.ReflectionOnly ? new ReflectionOnlyCustomAttributeBuilderBuilder() : new CustomAttributeBuilderBuilder(); var attribute = builder.Build(expression); parameter.SetCustomAttribute(attribute); }
public void TestThrowsExceptionForNullByteArray() { AssemblyName myAsmName = new AssemblyName("GenericEmitExample1"); AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run); ModuleBuilder myModule = TestLibrary.Utilities.GetModuleBuilder(myAssembly, myAsmName.Name); TypeBuilder myType = myModule.DefineType("Sample", TypeAttributes.Public); string[] typeParamNames = { "TFirst" }; GenericTypeParameterBuilder[] typeParams = myType.DefineGenericParameters(typeParamNames); GenericTypeParameterBuilder TFirst = typeParams[0]; ConstructorInfo con = typeof(HelperAttribute).GetConstructor(new Type[] { }); Assert.Throws <ArgumentNullException>(() => { TFirst.SetCustomAttribute(con, null); }); }
public void TestThrowsExceptionOnNullConstructorInfo() { AssemblyName myAsmName = new AssemblyName("GenericEmitExample1"); AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run); ModuleBuilder myModule = TestLibrary.Utilities.GetModuleBuilder(myAssembly, myAsmName.Name); Type baseType = typeof(ExampleBase); TypeBuilder myType = myModule.DefineType("Sample", TypeAttributes.Public); string[] typeParamNames = { "TFirst" }; GenericTypeParameterBuilder[] typeParams = myType.DefineGenericParameters(typeParamNames); GenericTypeParameterBuilder TFirst = typeParams[0]; byte[] binaryAttribute = new byte[128]; Assert.Throws <ArgumentNullException>(() => { TFirst.SetCustomAttribute(null, binaryAttribute); }); }
internal void CompileBaseDefinition(GenericTypeParameterBuilder genericTypeParameterBuilder) { VerificationBaseDefinition(true); State = Metadata.State.AllDefiniton; type = genericTypeParameterBuilder; if (type != null) { genericTypeParameterBuilder.SetBaseTypeConstraint(typeConstraint); } if (interfaces.Count() > 0) { genericTypeParameterBuilder.SetInterfaceConstraints(interfaces.Select(t => (Type)t).ToArray()); } foreach (CustomAttributeBuilder customAttribute in customAttributes) { genericTypeParameterBuilder.SetCustomAttribute(customAttribute); } State = Metadata.State.Defined; type = genericTypeParameterBuilder; }
public void TestSetCustomAttribute() { AssemblyName myAsmName = new AssemblyName("GenericEmitExample1"); AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run); ModuleBuilder myModule = TestLibrary.Utilities.GetModuleBuilder(myAssembly, myAsmName.Name); Type baseType = typeof(ExampleBase); TypeBuilder myType = myModule.DefineType("Sample", TypeAttributes.Public); string[] typeParamNames = { "TFirst" }; GenericTypeParameterBuilder[] typeParams = myType.DefineGenericParameters(typeParamNames); GenericTypeParameterBuilder TFirst = typeParams[0]; Type[] ctorParams = new Type[] { typeof(string) }; ConstructorInfo constructorinfo = typeof(HelperAttribute).GetConstructor(ctorParams); CustomAttributeBuilder cABB = new CustomAttributeBuilder(constructorinfo, new object[] { "TestString" }); TFirst.SetCustomAttribute(cABB); }
public void TestSetCustomAttribute() { AssemblyName myAsmName = new AssemblyName("GenericEmitExample1"); AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run); ModuleBuilder myModule = TestLibrary.Utilities.GetModuleBuilder(myAssembly, myAsmName.Name); Type baseType = typeof(ExampleBase); TypeBuilder myType = myModule.DefineType("Sample", TypeAttributes.Public); string[] typeParamNames = { "TFirst" }; GenericTypeParameterBuilder[] typeParams = myType.DefineGenericParameters(typeParamNames); GenericTypeParameterBuilder TFirst = typeParams[0]; ConstructorInfo[] cons = typeof(HelperAttribute).GetConstructors(); ConstructorInfo constructorinfo = cons[0]; byte[] binaryAttribute = new byte[128]; TFirst.SetCustomAttribute(constructorinfo, binaryAttribute); }