public void TestWithNullOnReturnTypeWithModifiersSetCorrectly() { string methodName = null; int arraySize = 0; methodName = TestLibrary.Generator.GetString(false, false, true, MinStringLength, MaxStringLength); arraySize = TestLibrary.Generator.GetByte(); TypeBuilder typeBuilder = GetTestTypeBuilder(); MethodBuilder builder = typeBuilder.DefineMethod(methodName, TestMethodAttributes); string[] typeParamNames = { "T" }; GenericTypeParameterBuilder[] typeParameters = builder.DefineGenericParameters(typeParamNames); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; Type[] desiredParamType = null; Type[][] parameterTypeRequiredCustomModifiers = new Type[arraySize][]; Type[][] parameterTypeOptionalCustomModifiers = new Type[arraySize][]; for (int i = 0; i < arraySize; ++i) { parameterTypeRequiredCustomModifiers[i] = null; parameterTypeOptionalCustomModifiers[i] = null; } builder.SetSignature( desiredReturnType.AsType(), null, null, desiredParamType, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers); }
public void TestGenericMethodWithParameterTypeOptionalModifierSet() { string methodName = null; methodName = TestLibrary.Generator.GetString(false, false, true, MinStringLength, MaxStringLength); TypeBuilder typeBuilder = GetTestTypeBuilder(); MethodBuilder builder = typeBuilder.DefineMethod(methodName, TestMethodAttributes); string[] typeParamNames = { "T" }; GenericTypeParameterBuilder[] typeParameters = builder.DefineGenericParameters(typeParamNames); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; Type[] desiredParamType = new Type[] { typeof(int) }; Type[][] parameterTypeOptionalCustomModifiers = new Type[desiredParamType.Length][]; for (int i = 0; i < desiredParamType.Length; ++i) { parameterTypeOptionalCustomModifiers[i] = null; } builder.SetSignature( desiredReturnType.AsType(), null, null, desiredParamType, null, parameterTypeOptionalCustomModifiers); VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); }
public void TestWithNullOnReturnTypeWithModifiersSetToWrongTypes() { string methodName = null; methodName = TestLibrary.Generator.GetString(false, false, true, MinStringLength, MaxStringLength); TypeBuilder typeBuilder = GetTestTypeBuilder(); MethodBuilder builder = typeBuilder.DefineMethod(methodName, TestMethodAttributes); string[] typeParamNames = { "T" }; GenericTypeParameterBuilder[] typeParameters = builder.DefineGenericParameters(typeParamNames); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; builder.SetSignature( null, null, null, null, null, null); VerifyMethodSignature(typeBuilder, builder, null); }
public void IsArray() { GenericTypeParameterBuilder typeParam = Helpers.DynamicType(TypeAttributes.Public).DefineGenericParameters("TFirst")[0]; Assert.False(typeParam.IsArray); Assert.False(typeParam.IsSZArray); Type asType = typeParam.AsType(); Assert.False(asType.IsArray); Assert.False(asType.IsSZArray); Type arrType = typeParam.MakeArrayType(); Assert.True(arrType.IsArray); Assert.True(arrType.IsSZArray); arrType = typeParam.MakeArrayType(1); Assert.True(arrType.IsArray); Assert.False(arrType.IsSZArray); arrType = typeParam.MakeArrayType(2); Assert.True(arrType.IsArray); Assert.False(arrType.IsSZArray); }
public void TestGenericMethodWithMultipleParametersSet() { string methodName = null; methodName = TestLibrary.Generator.GetString(false, false, true, MinStringLength, MaxStringLength); TypeBuilder typeBuilder = GetTestTypeBuilder(); MethodBuilder builder = typeBuilder.DefineMethod(methodName, TestMethodAttributes); string[] typeParamNames = { "T", "U" }; GenericTypeParameterBuilder[] typeParameters = builder.DefineGenericParameters(typeParamNames); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; Type[] desiredParamType = new Type[] { typeof(int), typeParameters[1].AsType() }; builder.SetSignature( desiredReturnType.AsType(), null, null, desiredParamType, null, null); VerifyMethodSignature(typeBuilder, builder, desiredReturnType.AsType()); }
private static void EmitOverride_WriteTMany(TypeBuilder tb, string name, StaticCacheDescription manyCacheDesc, FieldInfo mapperInstance) { MethodInfo miDeclaration = typeof(Writer).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance); MethodBuilder mb = tb.DefineMethod(name, MethodAttributes.Family | MethodAttributes.Virtual); GenericTypeParameterBuilder genTypeParam = mb.DefineGenericParameters("T")[0]; mb.SetReturnType(null); mb.SetParameters(typeof(uint), genTypeParam); tb.DefineMethodOverride(mb, miDeclaration); FieldInfo fi = manyCacheDesc.Instance(genTypeParam); ILGenerator il = mb.GetILGenerator(); Label noBuild = il.DefineLabel(); il.Emit(OpCodes.Ldsfld, fi); il.Emit(OpCodes.Brtrue, noBuild); il.Emit(OpCodes.Ldsfld, mapperInstance); il.Emit(OpCodes.Call, typeof(WriteMapper).GetMethod("BuildCaches", BindingFlags.Public | BindingFlags.Instance)); il.MarkLabel(noBuild); il.Emit(OpCodes.Ldsfld, fi); il.Emit(OpCodes.Ldarg_0); // this writer il.Emit(OpCodes.Ldarg_1); // len or max il.Emit(OpCodes.Ldarg_2); // item MethodInfo miInvoke = TypeBuilder.GetMethod(typeof(WriteManyDelegate <>).MakeGenericType(genTypeParam), typeof(WriteManyDelegate <>).GetMethod("Invoke")); il.Emit(OpCodes.Callvirt, miInvoke); il.Emit(OpCodes.Ret); }
public void DefineGenericParameters(string[] typeParamNames) { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); GenericTypeParameterBuilder[] genericParameters = type.DefineGenericParameters(typeParamNames); Assert.True(type.IsGenericType); Assert.True(type.IsGenericTypeDefinition); for (int i = 0; i < typeParamNames.Length; i++) { GenericTypeParameterBuilder parameter = genericParameters[i]; Assert.Equal(typeParamNames[i], parameter.Name); Assert.Null(parameter.Namespace); Assert.Null(parameter.FullName); Assert.Null(parameter.AssemblyQualifiedName); Assert.Equal(type.AsType(), parameter.DeclaringType); Assert.Null(parameter.DeclaringMethod); Assert.Equal(type.Module, parameter.Module); Assert.Equal(type.Assembly, parameter.Assembly); Assert.Null(parameter.BaseType); Assert.Equal(i, parameter.GenericParameterPosition); Assert.Equal(GenericParameterAttributes.None, parameter.GenericParameterAttributes); Assert.True(parameter.IsGenericParameter); Assert.False(parameter.IsGenericType); Assert.False(parameter.IsGenericTypeDefinition); Assert.True(parameter.ContainsGenericParameters); } }
private static MethodBuilder GetMethodBuilder_Instantiate(TypeBuilder tb, GenericTypeParameterBuilder K, ConstructorBuilder constructor, FieldBuilder fb_instance, FieldBuilder fb_cell) { MethodBuilder mb_Instantiate = tb.DefineMethod( "Instantiate", MethodAttributes.Public | MethodAttributes.Static, tb, null); ILGenerator mb_InstantiateIL = mb_Instantiate.GetILGenerator(); Label endOfIfBlock = mb_InstantiateIL.DefineLabel(); mb_InstantiateIL.Emit(OpCodes.Ldsfld, fb_instance); mb_InstantiateIL.Emit(OpCodes.Brtrue_S, endOfIfBlock); mb_InstantiateIL.Emit(OpCodes.Newobj, constructor); mb_InstantiateIL.Emit(OpCodes.Stsfld, fb_instance); mb_InstantiateIL.Emit(OpCodes.Ldsfld, fb_instance); mb_InstantiateIL.Emit(OpCodes.Ldc_I4_1); mb_InstantiateIL.Emit(OpCodes.Newarr, K); mb_InstantiateIL.Emit(OpCodes.Stfld, fb_cell); mb_InstantiateIL.MarkLabel(endOfIfBlock); mb_InstantiateIL.Emit(OpCodes.Ldsfld, fb_instance); mb_InstantiateIL.Emit(OpCodes.Ret); return(mb_Instantiate); }
private void EmitOverride_WriteTOne(TypeBuilder typeBuilder, FieldInfo mapperInstance) { MethodInfo miDeclaration = typeof(Writer).GetMethod("CacheWrite", BindingFlags.NonPublic | BindingFlags.Instance); MethodBuilder mb = typeBuilder.DefineMethod("CacheWrite", MethodAttributes.Family | MethodAttributes.Virtual); GenericTypeParameterBuilder genTypeParam = mb.DefineGenericParameters("T")[0]; mb.SetReturnType(null); mb.SetParameters(genTypeParam); typeBuilder.DefineMethodOverride(mb, miDeclaration); FieldInfo fi = TypeBuilder.GetField(_oneCacheDescription.Result.MakeGenericType(genTypeParam), _oneCacheDescription.Result.GetField("Instance")); ILGenerator il = mb.GetILGenerator(); Label noBuild = il.DefineLabel(); il.Emit(OpCodes.Ldsfld, fi); il.Emit(OpCodes.Brtrue, noBuild); il.Emit(OpCodes.Ldsfld, mapperInstance); il.Emit(OpCodes.Call, typeof(WriteMapper).GetMethod("BuildCaches", BindingFlags.Public | BindingFlags.Instance)); il.MarkLabel(noBuild); il.Emit(OpCodes.Ldsfld, fi); il.Emit(OpCodes.Ldarg_0); // this writer il.Emit(OpCodes.Ldarg_1); // item MethodInfo miInvoke = TypeBuilder.GetMethod(typeof(WriteOneDelegate <>).MakeGenericType(genTypeParam), typeof(WriteOneDelegate <>).GetMethod("Invoke")); il.Emit(OpCodes.Callvirt, miInvoke); il.Emit(OpCodes.Ret); }
private static PropertyBuilder GetPropertyBuilder_Cell(TypeBuilder tb, GenericTypeParameterBuilder K, FieldBuilder fb_cell) { PropertyBuilder pb_Cell = tb.DefineProperty("Cell", PropertyAttributes.HasDefault, K.MakeArrayType(), null); MethodBuilder cellGetter = tb.DefineMethod( "get_Cell", MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual, K.MakeArrayType(), Type.EmptyTypes); ILGenerator cellGetterIL = cellGetter.GetILGenerator(); cellGetterIL.Emit(OpCodes.Ldarg_0); cellGetterIL.Emit(OpCodes.Ldfld, fb_cell); cellGetterIL.Emit(OpCodes.Ret); MethodBuilder cellSetter = tb.DefineMethod( "set_Cell", MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual, null, new Type[] { K.MakeArrayType() }); ILGenerator cellSetterIL = cellSetter.GetILGenerator(); cellSetterIL.Emit(OpCodes.Ldarg_0); cellSetterIL.Emit(OpCodes.Ldarg_1); cellSetterIL.Emit(OpCodes.Stfld, fb_cell); cellSetterIL.Emit(OpCodes.Ret); pb_Cell.SetGetMethod(cellGetter); pb_Cell.SetSetMethod(cellSetter); return(pb_Cell); }
public virtual void CreateGetGenericByIntMethod(TypeBuilder tb) { string[] typeParameterNames = { "V" }; GenericTypeParameterBuilder[] typeParameters = tb.DefineGenericParameters(typeParameterNames); GenericTypeParameterBuilder V = typeParameters[0]; MethodInfo mi = typeof(IFigure).GetMethod("Get", new Type[] { typeof(int) }).MakeGenericMethod(typeParameters); ParameterInfo[] args = mi.GetParameters(); Type[] argTypes = Array.ConvertAll(args, a => a.ParameterType); MethodBuilder method = tb.DefineMethod(mi.Name, mi.Attributes & ~MethodAttributes.Abstract, mi.CallingConvention, mi.ReturnType, argTypes); tb.DefineMethodOverride(method, mi); ILGenerator il = method.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldflda, fields[0]); il.Emit(OpCodes.Ldarg_1); il.EmitCall(OpCodes.Call, typeof(Ussn).GetMethod("CompareTo", new Type[] { typeof(IUnique) }), null); il.Emit(OpCodes.Ret); }
private static void ApplyGenericConstraints(Type genericArgument, GenericTypeParameterBuilder genericTypeParameter) { var genericConstraints = genericArgument.GetTypeInfo().GetGenericParameterConstraints(); genericTypeParameter.SetInterfaceConstraints(genericConstraints.Where(gc => gc.GetTypeInfo().IsInterface).ToArray()); genericTypeParameter.SetBaseTypeConstraint(genericConstraints.FirstOrDefault(t => t.GetTypeInfo().IsClass)); }
private static void BuildGenericType() { //Define assembly AppDomain dom = AppDomain.CurrentDomain; AssemblyName asmName = new AssemblyName("domath"); AssemblyBuilder asm = dom.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave); //Define a dynamic module ModuleBuilder mod = asm.DefineDynamicModule(asmName.Name, asmName.Name + ".dll"); //Define a class TypeBuilder asmType = mod.DefineType("OurClass", TypeAttributes.Public); //Define the generic type parameters string[] typeNames = { "TFirst", "TSecond" }; GenericTypeParameterBuilder[] genTypes = asmType.DefineGenericParameters(typeNames); GenericTypeParameterBuilder TFirst = genTypes[0]; GenericTypeParameterBuilder TSecond = genTypes[1]; //Define generic constraints TFirst.SetGenericParameterAttributes(GenericParameterAttributes.DefaultConstructorConstraint | GenericParameterAttributes.ReferenceTypeConstraint); TSecond.SetBaseTypeConstraint(typeof(SomeBaseClass)); Type[] interfaceTypes = { typeof(InterfaceA), typeof(InterfaceB) }; TSecond.SetInterfaceConstraints(interfaceTypes); //Define a field FieldBuilder fld1 = asmType.DefineField("Field1", TFirst, FieldAttributes.Private); //Define method Type listOf = typeof(List <>); Type listOfTFirst = listOf.MakeGenericType(TFirst); Type[] paramTypes = { TFirst.MakeArrayType() }; MethodBuilder asmMethod = asmType.DefineMethod("SomeMethod", MethodAttributes.Public | MethodAttributes.Static, listOfTFirst, paramTypes); //Define Method Body ILGenerator il = asmMethod.GetILGenerator(); Type ienumOf = typeof(IEnumerable <>); Type tFromListOf = listOf.GetGenericArguments()[0]; Type ienumOfT = ienumOf.MakeGenericType(tFromListOf); Type[] ctorArgs = { ienumOfT }; ConstructorInfo ctorPrep = listOf.GetConstructor(ctorArgs); ConstructorInfo ctor = TypeBuilder.GetConstructor(listOfTFirst, ctorPrep); il.Emit(OpCodes.Ldarg_0); //Loads the argument at index 0 onto the evaluation stack. il.Emit(OpCodes.Newobj, ctor); //Creates a new object or a new instance of a value type, pushing an object reference (type O) onto the evaluation stack. il.Emit(OpCodes.Ret); //Returns from the current method, pushing a return value (if present) from the callee's evaluation stack onto the caller's evaluation stack. //Create type and save file Type finished = asmType.CreateType(); asm.Save(asmName.Name + ".dll"); }
protected virtual Type GetStartInfoBaseType(GenericTypeParameterBuilder parameterType) { return(typeof(ServiceSubprocessStartInfoBase <, ,>) .MakeGenericType( UserServiceContract, GeneratedServiceContract, parameterType)); }
private static void CreateEnumEnum(ModuleBuilder modb, ClassLoaderWrapper loader) { TypeBuilder tb = modb.DefineType(DotNetTypeWrapper.GenericEnumEnumTypeName, TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Public); GenericTypeParameterBuilder gtpb = tb.DefineGenericParameters("T")[0]; gtpb.SetBaseTypeConstraint(Types.Enum); genericEnumEnumType = tb; }
private static void CopyNonInheritableAttributes(GenericTypeParameterBuilder newGenericParameter, Type originalGenericArgument) { foreach (var attribute in originalGenericArgument.GetNonInheritableAttributes()) { newGenericParameter.SetCustomAttribute(attribute.Builder); } }
/// <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); }
protected override Type GetStartInfoBaseType(GenericTypeParameterBuilder parameterType) { return(typeof(DuplexSubprocessStartInfoBase <, , , ,>) .MakeGenericType( UserServiceContract, GeneratedServiceContract, _UserCallbackContract, CallbackContractType, parameterType)); }
public void SetParent_GenericParameter_ThrowsNotSupportedExceptionOnCreation() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); GenericTypeParameterBuilder genericType = type.DefineGenericParameters("T")[0]; type.SetParent(genericType.AsType()); Assert.Equal(genericType.AsType(), type.BaseType); Assert.Throws <NotSupportedException>(() => type.CreateTypeInfo()); }
public void SetSignature_GenericMethod_ReturnType_OptionalCustomModifiers() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; method.SetSignature(desiredReturnType.AsType(), null, null, null, null, null); VerifyMethodSignature(type, method, desiredReturnType.AsType()); }
public void SetGenericParameterAttributes(GenericParameterAttributes genericParameterAttributes) { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); string[] typeParamNames = new string[] { "TFirst" }; GenericTypeParameterBuilder[] typeParams = type.DefineGenericParameters(typeParamNames); GenericTypeParameterBuilder firstTypeParam = typeParams[0]; firstTypeParam.SetGenericParameterAttributes(genericParameterAttributes); Assert.Equal(genericParameterAttributes, firstTypeParam.GenericParameterAttributes); }
public void SetSignature_NullReturnType_CustomModifiersSetToWrongTypes() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; method.SetSignature(null, null, null, null, null, null); VerifyMethodSignature(type, method, typeof(void)); }
public void SetSignature_GenericMethod_SingleGenericParameter() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder returnType = typeParameters[0]; method.SetSignature(returnType.AsType(), null, null, null, null, null); VerifyMethodSignature(type, method, returnType.AsType()); }
private static Type CreateCustomType(int size) { TypeAttributes attrs = TypeAttributes.NotPublic | TypeAttributes.SequentialLayout; TypeBuilder type = UnsafeMethods.DynamicModule.DefineType("VariantArray" + size, attrs, typeof(ValueType)); GenericTypeParameterBuilder T = type.DefineGenericParameters(new string[] { "T" })[0]; for (int i = 0; i < size; i++) { type.DefineField("Element" + i, T, FieldAttributes.Public); } return(type.CreateType()); }
static MethodBuilder CreateInterfaceMethodBuilder(TypeBuilder typeBuilder, MethodInfo m, out Type[] parameters) { // Initializes the signature with only its name, attributes and calling conventions first. MethodBuilder mB = typeBuilder.DefineMethod( m.Name, MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.Final, CallingConventions.HasThis); parameters = ReflectionHelper.CreateParametersType(m.GetParameters()); // If it is a Generic method definition (since we are working with an interface, // it can not be a Generic closed nor opened method). if (m.IsGenericMethodDefinition) { Type[] genArgs = m.GetGenericArguments(); Debug.Assert(genArgs.Length > 0); string[] names = new string[genArgs.Length]; for (int i = 0; i < names.Length; ++i) { names[i] = genArgs[i].Name; } // Defines generic parameters. GenericTypeParameterBuilder[] genTypes = mB.DefineGenericParameters(names); for (int i = 0; i < names.Length; ++i) { Type source = genArgs[i]; GenericTypeParameterBuilder target = genTypes[i]; target.SetGenericParameterAttributes(source.GenericParameterAttributes); Type[] sourceConstraints = source.GetGenericParameterConstraints(); List <Type> interfaces = new List <Type>(); foreach (Type c in sourceConstraints) { if (c.IsClass) { target.SetBaseTypeConstraint(c); } else { interfaces.Add(c); } } target.SetInterfaceConstraints(interfaces.ToArray()); } } // Now that generic parameters have been defined, configures the signature. mB.SetReturnType(m.ReturnType); mB.SetParameters(parameters); // Set DebuggerStepThroughAttribute. ConstructorInfo ctor = typeof(DebuggerStepThroughAttribute).GetConstructor(Type.EmptyTypes); CustomAttributeBuilder attr = new CustomAttributeBuilder(ctor, new object[0]); mB.SetCustomAttribute(attr); return(mB); }
public void SetBaseTypeConstraint(Type baseTypeConstraint, Type expectedBaseType) { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); string[] typeParamNames = new string[] { "TFirst" }; GenericTypeParameterBuilder[] typeParams = type.DefineGenericParameters(typeParamNames); GenericTypeParameterBuilder firstParam = typeParams[0]; firstParam.SetBaseTypeConstraint(baseTypeConstraint); Assert.Equal(expectedBaseType, firstParam.BaseType); }
public Type EmitGenericType(string typeName, Type genericArgumentType) { var assemblyName = new AssemblyName("EmittedTestAssembly"); var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); var moduleBuilder = assemblyBuilder.DefineDynamicModule("EmittedTestModule"); var typeBuilder = moduleBuilder.DefineType(typeName, TypeAttributes.Public); GenericTypeParameterBuilder T = typeBuilder.DefineGenericParameters(new string[] { "T" })[0]; typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(ExportAttribute).GetConstructor(Type.EmptyTypes), new object[0])); return(typeBuilder.CreateType().MakeGenericType(genericArgumentType)); }
public void SetSignature_GenericMethod_MultipleParameters() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T", "U"); GenericTypeParameterBuilder desiredReturnType = typeParameters[0]; Type[] desiredParamType = new Type[] { typeof(int), typeParameters[1].AsType() }; method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, null, null); VerifyMethodSignature(type, method, desiredReturnType.AsType()); }
Type GetDimensionalType(string typeName, string templateName = null, int dimensions = 0, GenericTypeParameterBuilder templateType = null) { var baseType = GetTemplatedType(typeName, templateName, templateType); if (dimensions == 0) { return(baseType); } return(baseType.MakeArrayType(dimensions)); }
public void ToString_AllFieldsSet() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); ILGenerator ilGenerator = method.GetILGenerator(); ilGenerator.Emit(OpCodes.Ret); GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T"); GenericTypeParameterBuilder returnType = typeParameters[0]; method.SetSignature(returnType.AsType(), null, null, null, null, null); Assert.Contains(ExpectedToStrin(method), method.ToString()); }
private static void VerifyGenericArguments(MethodBuilder method, GenericTypeParameterBuilder[] expected) { Type[] genericArguments = method.GetGenericArguments(); if (expected == null) { Assert.Null(genericArguments); } else { Assert.Equal(expected.Length, genericArguments.Length); for (int i = 0; i < genericArguments.Length; ++i) { Assert.True(expected[i].Equals(genericArguments[i])); } } }
private static void VerifyGenericArguments(MethodBuilder method, GenericTypeParameterBuilder[] expected) { Type[] genericArguments = method.GetGenericArguments(); Assert.Equal(expected.Select(p => p.AsType()), genericArguments); }