public DefineGenericParameters ( ) : System.Reflection.Emit.GenericTypeParameterBuilder[] | ||
return | System.Reflection.Emit.GenericTypeParameterBuilder[] |
private static void MakeGenericType(Type baseType, TypeBuilder typeBuilder) { Type[] genericArguments = baseType.GetGenericArguments(); string[] genericArgumentNames = genericArguments.Select(g => g.Name).ToArray(); var genericTypeParameterBuilder = typeBuilder.DefineGenericParameters(genericArgumentNames); typeBuilder.MakeGenericType(genericTypeParameterBuilder); }
/// <summary> /// Makes the typeBuilder a generic. /// </summary> /// <param name="concrete">The concrete.</param> /// <param name="typeBuilder">The type builder.</param> public static void MakeGenericType(Type baseType, TypeBuilder typeBuilder) { Type[] genericArguments = baseType.GetGenericArguments(); string[] genericArgumentNames = GetArgumentNames(genericArguments); GenericTypeParameterBuilder[] genericTypeParameterBuilder = typeBuilder.DefineGenericParameters(genericArgumentNames); typeBuilder.MakeGenericType(genericTypeParameterBuilder); }
public static GenericTypeParameterBuilder[] DefineGenericArguments( Type[] genericArguments, TypeBuilder builder, Dictionary<String, GenericTypeParameterBuilder> name2GenericType) { return DefineGenericArguments(genericArguments, name2GenericType, delegate(String[] args) { return builder.DefineGenericParameters(args); }); }
public static GenericTypeParameterBuilder[] CopyGenericArguments( MethodInfo methodToCopyGenericsFrom, TypeBuilder builder, Dictionary<String, GenericTypeParameterBuilder> name2GenericType) { return CopyGenericArguments(methodToCopyGenericsFrom, name2GenericType, delegate(String[] args) { return builder.DefineGenericParameters(args); }); }
internal static Type MakeGenericType(this Type type, TypeBuilder typeBuilder) { GenericTypeParameterBuilder[] genericTypeParameterBuilder = null; Type[] genericArguments = type.GetGenericArguments(); string[] genericParameters = genericArguments.Select(g => g.Name) .ToArray(); genericTypeParameterBuilder = typeBuilder.DefineGenericParameters(genericParameters); return typeBuilder.MakeGenericType(genericTypeParameterBuilder); }
void SetUp (AssemblyBuilderAccess access) { AssemblyName assemblyName = new AssemblyName (); assemblyName.Name = ASSEMBLY_NAME; assembly = AppDomain.CurrentDomain.DefineDynamicAssembly ( assemblyName, access, Path.GetTempPath ()); module = assembly.DefineDynamicModule ("module1"); tb = module.DefineType ("Bar"); GenericTypeParameterBuilder [] typeParams = tb.DefineGenericParameters ("T"); cb = tb.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard, new Type [] { typeof (string), typeof (int) }); ILGenerator ig = cb.GetILGenerator (); ig.Emit (OpCodes.Ret); typeBarOfInt32 = tb.MakeGenericType (typeof (int)); ci = TypeBuilder.GetConstructor (typeBarOfInt32, cb); }
private void CompileInGeneratingFunction() { InClass = utilityClass.DefineNestedType(Constants.InGeneratingFunctionClassName, TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic); var genericParameters = declaration.TypeParameters.Any() ? InClass.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; var greatestFixedPoint = declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(genericParameters) : TypeBuilder; var fGreatestFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, greatestFixedPoint, genericParameters, runtimeContainer); InClass.AddInterfaceImplementation(GreatestFixedPointFunction.MakeGenericType(new[] { fGreatestFixedPoint }.Concat(genericParameters).ToArray())); InGeneratingFunctionConstructor = InClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, TypeBuilder.EmptyTypes); var inCtorBody = InGeneratingFunctionConstructor.GetILGenerator(); inCtorBody.Emit(OpCodes.Ldarg_0); inCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]); inCtorBody.Emit(OpCodes.Ret); var apply = InClass.DefineMethod(Constants.ApplyMethodName, MethodAttributes.Public | MethodAttributes.Virtual, fGreatestFixedPoint, TypeBuilder.EmptyTypes); var genericParameter = apply.DefineGenericParameters(Constants.ApplyMethodGenericParameterName)[0]; var fGenericParameter = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, genericParameter, genericParameters, runtimeContainer); apply.SetParameters(genericParameter, typeof(IFunction<,>).MakeGenericType(genericParameter, fGenericParameter)); var applyBody = apply.GetILGenerator(); applyBody.Emit(OpCodes.Ldarg_2); applyBody.Emit(OpCodes.Newobj, TypeBuilder.GetConstructor( AnaFunction.MakeGenericType(new[] { genericParameter }.Concat(genericParameters).ToArray()), AnaFunctionConstructor)); applyBody.Emit(OpCodes.Call, fmap.MakeGenericMethod(new Type[] { genericParameter, greatestFixedPoint }.Concat(genericParameters).ToArray())); applyBody.Emit(OpCodes.Ldarg_2); applyBody.Emit(OpCodes.Ldarg_1); applyBody.Emit(OpCodes.Callvirt, TypeBuilder.GetMethod( typeof(IFunction<,>).MakeGenericType(genericParameter, fGenericParameter), typeof(IFunction<,>).GetMethod(Constants.CallMethodName))); applyBody.Emit(OpCodes.Callvirt, TypeBuilder.GetMethod( typeof(IFunction<,>).MakeGenericType(fGenericParameter, fGreatestFixedPoint), typeof(IFunction<,>).GetMethod(Constants.CallMethodName))); applyBody.Emit(OpCodes.Ret); }
private static void DefineGenericArguments(TypeBuilder typeBuilder, Type baseClass) { if (!baseClass.IsGenericType) return; Type[] genericArguments = baseClass.GetGenericArguments(); GenericTypeParameterBuilder[] genericTypes = typeBuilder.DefineGenericParameters( genericArguments.Select(t => t.Name).ToArray()); for (int i = 0; i < genericArguments.Length; ++i) { genericTypes[i].SetGenericParameterAttributes(genericArguments[i].GenericParameterAttributes); foreach (Type constraint in genericArguments[i].GetGenericParameterConstraints()) { genericTypes[i].SetBaseTypeConstraint(constraint); } } }
private GenericTypeParameterBuilder[] DefineGenericParameters(TypeBuilder typeBuilder, MethodBuilder methodBuilder, IEnumerable<Cci.IGenericParameter> typeParameters) { Debug.Assert(typeBuilder != null ^ methodBuilder != null); var names = new List<string>(typeParameters.Select(typeParameter => typeParameter.Name)).ToArray(); var gpBuilders = (typeBuilder != null) ? typeBuilder.DefineGenericParameters(names) : methodBuilder.DefineGenericParameters(names); int i = 0; foreach (var typeParameter in typeParameters) { _genericParameterBuilders.Add(typeParameter, gpBuilders[i++]); } return gpBuilders; }
private static GenericParameterMapper DefineGenericArguments(TypeBuilder typeBuilder, Type baseClass) { if (!baseClass.IsGenericType) { return GenericParameterMapper.DefaultMapper; } Type[] genericArguments = baseClass.GetGenericArguments(); GenericTypeParameterBuilder[] genericTypes = typeBuilder.DefineGenericParameters( genericArguments.Select(t => t.Name).ToArray()); for (int i = 0; i < genericArguments.Length; ++i) { genericTypes[i].SetGenericParameterAttributes(genericArguments[i].GenericParameterAttributes); var interfaceConstraints = new List<Type>(); foreach (Type constraint in genericArguments[i].GetGenericParameterConstraints()) { if (constraint.IsClass) { genericTypes[i].SetBaseTypeConstraint(constraint); } else { interfaceConstraints.Add(constraint); } } if (interfaceConstraints.Count > 0) { genericTypes[i].SetInterfaceConstraints(interfaceConstraints.ToArray()); } } return new GenericParameterMapper(genericArguments, genericTypes.Cast<Type>().ToArray()); }
private void CompileOutClass() { OutClass = utilityClass.DefineNestedType(Constants.OutClassName, TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic, null, new[] { TypeBuilder }); var genericParameters = declaration.TypeParameters.Any() ? OutClass.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; var fLeastFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, TypeBuilder, genericParameters, runtimeContainer); var predField = OutClass.DefineField(Constants.OutClassPredFieldName, fLeastFixedPoint, FieldAttributes.Private); OutClassConstructor = OutClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { fLeastFixedPoint }); var outCtorBody = OutClassConstructor.GetILGenerator(); outCtorBody.Emit(OpCodes.Ldarg_0); outCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]); outCtorBody.Emit(OpCodes.Ldarg_0); outCtorBody.Emit(OpCodes.Ldarg_1); outCtorBody.Emit(OpCodes.Stfld, predField); outCtorBody.Emit(OpCodes.Ret); var cata = OutClass.DefineMethod(Constants.CataMethodName, MethodAttributes.Public | MethodAttributes.Virtual); var genericParameter = cata.DefineGenericParameters(Constants.CataMethodGenericParameterName)[0]; var fGenericParameter = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, genericParameter, genericParameters, runtimeContainer); cata.SetParameters(typeof(IFunction<,>).MakeGenericType(fGenericParameter, genericParameter)); cata.SetReturnType(genericParameter); var cataBody = cata.GetILGenerator(); cataBody.Emit(OpCodes.Ldarg_1); cataBody.Emit(OpCodes.Ldarg_1); cataBody.Emit(OpCodes.Newobj, TypeBuilder.GetConstructor(CataFunction.MakeGenericType(new[] { genericParameter }.Concat(genericParameters).ToArray()), CataFunctionConstructor)); cataBody.Emit(OpCodes.Call, fmap.MakeGenericMethod(new Type[] { TypeBuilder, genericParameter }.Concat(genericParameters).ToArray())); cataBody.Emit(OpCodes.Ldarg_0); cataBody.Emit(OpCodes.Ldfld, predField); cataBody.Emit(OpCodes.Callvirt, TypeBuilder.GetMethod( typeof(IFunction<,>).MakeGenericType(fLeastFixedPoint, fGenericParameter), typeof(IFunction<,>).GetMethod(Constants.CallMethodName))); cataBody.Emit(OpCodes.Callvirt, TypeBuilder.GetMethod( typeof(IFunction<,>).MakeGenericType(fGenericParameter, genericParameter), typeof(IFunction<,>).GetMethod(Constants.CallMethodName))); cataBody.Emit(OpCodes.Ret); }
protected override void PreBuild() { if(Info != null) { return; } var cont = CurrentContainer; Builder = cont.CreateType(Name, Attributes); Info = Builder; if (Generics.Count > 0) { var gb = Builder.DefineGenericParameters(Generics.ToNames()); Generics.RegisterBuilders(gb); } if (BaseType != null) { Builder.SetParent(BaseType.GainType()); //todo ジェネリクスに対応したTypeを生成する。 } Builder.AddImplements(Implements); }
private void AllocateGenericParametersIfNecessary(ITypeDefinition typeDefinition, TypeBuilder typeBuilder) { if (!typeDefinition.IsGeneric) return; //We don't need these to be allocated in this pass, but it is more convenient to do it here. var names = new string[typeDefinition.GenericParameterCount]; foreach (var genericParameter in typeDefinition.GenericParameters) names[genericParameter.Index] = genericParameter.Name.Value; var genericParameterBuilders = typeBuilder.DefineGenericParameters(names); foreach (var genericParameter in typeDefinition.GenericParameters) { var genericParameterBuilder = genericParameterBuilders[genericParameter.Index]; this.loader.builderMap.Add(genericParameter, genericParameterBuilder); this.loader.mapper.DefineMapping(genericParameter, genericParameterBuilder); } }
public void Compile() { GreatestFixedPointFunction = module.DefineType(moduleName + '.' + Constants.TypesNamespace + '.' + name + Constants.FunctionSuffix, TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract); var functionClassGenericParameters = GreatestFixedPointFunction.DefineGenericParameters(new[] { Constants.GFixFunctionClassGenericParameterName }.Concat(declaration.TypeParameters).ToArray()); GreatestFixedPointFunctionApplyMethod = GreatestFixedPointFunction.DefineMethod(Constants.ApplyMethodName, MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); var underlyingType = GreatestFixedPointFunctionApplyMethod.DefineGenericParameters(Constants.ApplyMethodGenericParameterName)[0]; var functorClass = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, underlyingType, functionClassGenericParameters.Skip(1).ToArray(), runtimeContainer); GreatestFixedPointFunctionApplyMethod.SetParameters(underlyingType, typeof(IFunction<,>).MakeGenericType(underlyingType, functorClass)); GreatestFixedPointFunctionApplyMethod.SetReturnType(functionClassGenericParameters[0]); TypeBuilder = module.DefineType(moduleName + '.' + Constants.TypesNamespace + '.' + name, TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract); var typeGenericParameters = declaration.TypeParameters.Any() ? TypeBuilder.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; GreatestFixedPointApplyMethod = TypeBuilder.DefineMethod(Constants.ApplyMethodName, MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); var resultTypeGfix = GreatestFixedPointApplyMethod.DefineGenericParameters(Constants.GFixFunctionClassGenericParameterName)[0]; GreatestFixedPointApplyMethod.SetParameters(GreatestFixedPointFunction.MakeGenericType(new[] { resultTypeGfix }.Concat(typeGenericParameters).ToArray())); GreatestFixedPointApplyMethod.SetReturnType(resultTypeGfix); CompileAnaClass(); CompileAna(); CompileAnaFunction(); CompileAnaFunction1(); CompileIn(); CompileInFunction(); CompileOut(); CompileOutFunction(); TypeBuilder.SetCustomAttribute(new CustomAttributeBuilder( typeof(InfiniteAttribute).GetConstructors()[0], new object[] { declaration.ConstructorFunctionName, declaration.DestructorFunctionName, declaration.AnaFunctionName, InFunction })); }
private void CompileCataFunction() { CataFunction = utilityClass.DefineNestedType(Constants.CataFunctionClassName, TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic, null, TypeBuilder.EmptyTypes); var genericParameters = CataFunction.DefineGenericParameters(new[] { Constants.CataFunctionClassGenericParameterName }.Concat(declaration.TypeParameters).ToArray()); var inputParameter = declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(genericParameters.Skip(1).ToArray()) : TypeBuilder; CataFunction.AddInterfaceImplementation(typeof(IFunction<,>).MakeGenericType(inputParameter, genericParameters[0])); var fSeedType = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, genericParameters[0], genericParameters, runtimeContainer); var algebra = typeof(IFunction<,>).MakeGenericType(fSeedType, genericParameters[0]); var seedField = CataFunction.DefineField(Constants.CataFunctionClassSeedFieldName, algebra, FieldAttributes.Private); CataFunctionConstructor = CataFunction.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { algebra }); var cataCtorBody = CataFunctionConstructor.GetILGenerator(); cataCtorBody.Emit(OpCodes.Ldarg_0); cataCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]); cataCtorBody.Emit(OpCodes.Ldarg_0); cataCtorBody.Emit(OpCodes.Ldarg_1); cataCtorBody.Emit(OpCodes.Stfld, seedField); cataCtorBody.Emit(OpCodes.Ret); var call = CataFunction.DefineMethod(Constants.CallMethodName, MethodAttributes.Public | MethodAttributes.Virtual, genericParameters[0], new Type[] { inputParameter }); var callBody = call.GetILGenerator(); callBody.Emit(OpCodes.Ldarg_1); callBody.Emit(OpCodes.Ldarg_0); callBody.Emit(OpCodes.Ldfld, seedField); callBody.Emit(OpCodes.Callvirt, genericParameters.Skip(1).Any() ? TypeBuilder.GetMethod( TypeBuilder.MakeGenericType(genericParameters.Skip(1).ToArray()), Cata).MakeGenericMethod(genericParameters[0]) : Cata.MakeGenericMethod(genericParameters[0])); callBody.Emit(OpCodes.Ret); }
/// <summary> /// Defines the generic parameters of an internal generic type. /// </summary> void DefineGenericParameters(TypeBuilder builder, GenericParameterDeclaration[] parameters) { string[] names = Array.ConvertAll<GenericParameterDeclaration, string>( parameters, delegate(GenericParameterDeclaration gpd) { return gpd.Name; }); GenericTypeParameterBuilder[] builders = builder.DefineGenericParameters(names); DefineGenericParameters(builders, parameters); }
private void CompileAnaFunction1() { AnaFunction1 = utilityClass.DefineNestedType(Constants.AnaFunction1ClassName, TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic, null, TypeBuilder.EmptyTypes); var anaClassGenericParameters = AnaFunction1.DefineGenericParameters(new[] { Constants.AnaFunction1ClassGenericParameterName }.Concat(declaration.TypeParameters).ToArray()); var resultType = declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(anaClassGenericParameters.Skip(1).ToArray()) : TypeBuilder; var fAnaClassGenericParameter = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, anaClassGenericParameters[0], anaClassGenericParameters.Skip(1).ToArray(), runtimeContainer); var coalgebraType = typeof(IFunction<,>).MakeGenericType(anaClassGenericParameters[0], fAnaClassGenericParameter); var terminalMorphismType = typeof(IFunction<,>).MakeGenericType(anaClassGenericParameters[0], resultType); AnaFunction1.AddInterfaceImplementation(typeof(IFunction<,>).MakeGenericType(coalgebraType, terminalMorphismType)); AnaFunction1Constructor = AnaFunction1.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, TypeBuilder.EmptyTypes); var anaCtorBody = AnaFunction1Constructor.GetILGenerator(); anaCtorBody.Emit(OpCodes.Ldarg_0); anaCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]); anaCtorBody.Emit(OpCodes.Ret); var call = AnaFunction1.DefineMethod(Constants.CallMethodName, MethodAttributes.Public | MethodAttributes.Virtual, terminalMorphismType, new Type[] { coalgebraType }); var callBody = call.GetILGenerator(); callBody.Emit(OpCodes.Ldarg_1); callBody.Emit(OpCodes.Newobj, TypeBuilder.GetConstructor( AnaFunction.MakeGenericType(anaClassGenericParameters), AnaFunctionConstructor)); callBody.Emit(OpCodes.Ret); }
private void CompileAnaFunction() { AnaFunction = utilityClass.DefineNestedType(Constants.AnaFunctionClassName, TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic, null, TypeBuilder.EmptyTypes); var anaClassGenericParameters = AnaFunction.DefineGenericParameters(new[] { Constants.AnaFunctionClassGenericParameterName }.Concat(declaration.TypeParameters).ToArray()); var resultType = declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(anaClassGenericParameters.Skip(1).ToArray()) : TypeBuilder; AnaFunction.AddInterfaceImplementation(typeof(IFunction<,>).MakeGenericType(anaClassGenericParameters[0], resultType)); var seedType = typeof(IFunction<,>).MakeGenericType(anaClassGenericParameters[0], FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, anaClassGenericParameters[0], anaClassGenericParameters.Skip(1).ToArray(), runtimeContainer)); var seedField = AnaFunction.DefineField(Constants.AnaFunctionClassSeedFieldName, seedType, FieldAttributes.Private); AnaFunctionConstructor = AnaFunction.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { seedType }); var anaCtorBody = AnaFunctionConstructor.GetILGenerator(); anaCtorBody.Emit(OpCodes.Ldarg_0); anaCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]); anaCtorBody.Emit(OpCodes.Ldarg_0); anaCtorBody.Emit(OpCodes.Ldarg_1); anaCtorBody.Emit(OpCodes.Stfld, seedField); anaCtorBody.Emit(OpCodes.Ret); var call = AnaFunction.DefineMethod(Constants.CallMethodName, MethodAttributes.Public | MethodAttributes.Virtual, resultType, new[] { anaClassGenericParameters[0] }); var callBody = call.GetILGenerator(); callBody.Emit(OpCodes.Ldarg_1); callBody.Emit(OpCodes.Ldarg_0); callBody.Emit(OpCodes.Ldfld, seedField); callBody.Emit(OpCodes.Call, Ana.MakeGenericMethod(anaClassGenericParameters)); callBody.Emit(OpCodes.Ret); }
private void CompileAnaClass() { AnaClass = utilityClass.DefineNestedType(Constants.AnaClassName, TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic, null, TypeBuilder.EmptyTypes); var anaClassGenericParameters = AnaClass.DefineGenericParameters(new[] { Constants.AnaClassGenericParameterName }.Concat(declaration.TypeParameters).ToArray()); if (declaration.TypeParameters.Any()) { AnaClass.AddInterfaceImplementation(TypeBuilder.MakeGenericType(anaClassGenericParameters.Skip(1).ToArray())); } else { AnaClass.AddInterfaceImplementation(TypeBuilder); } var anaClassGeneratorType = typeof(IFunction<,>).MakeGenericType(anaClassGenericParameters[0], FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, anaClassGenericParameters[0], anaClassGenericParameters.Skip(1).ToArray(), runtimeContainer)); var seedField = AnaClass.DefineField(Constants.AnaClassSeedFieldName, anaClassGenericParameters[0], FieldAttributes.Private); var generatorField = AnaClass.DefineField(Constants.AnaClassGeneratorFieldName, anaClassGeneratorType, FieldAttributes.Private); AnaClassConstructor = AnaClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { anaClassGenericParameters[0], anaClassGeneratorType }); var ctorBody = AnaClassConstructor.GetILGenerator(); ctorBody.Emit(OpCodes.Ldarg_0); ctorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]); ctorBody.Emit(OpCodes.Ldarg_0); ctorBody.Emit(OpCodes.Ldarg_1); ctorBody.Emit(OpCodes.Stfld, seedField); ctorBody.Emit(OpCodes.Ldarg_0); ctorBody.Emit(OpCodes.Ldarg_2); ctorBody.Emit(OpCodes.Stfld, generatorField); ctorBody.Emit(OpCodes.Ret); var apply = AnaClass.DefineMethod(Constants.ApplyMethodName, MethodAttributes.Public | MethodAttributes.Virtual); var resultType = apply.DefineGenericParameters(Constants.GFixFunctionClassGenericParameterName)[0]; var applyFunctionGenericParameters = new[] { resultType }.Concat(anaClassGenericParameters.Skip(1)).ToArray(); apply.SetParameters(GreatestFixedPointFunction.MakeGenericType(applyFunctionGenericParameters)); apply.SetReturnType(resultType); var applyMethodGenericClass = TypeBuilder.GetMethod( GreatestFixedPointFunction.MakeGenericType(applyFunctionGenericParameters), GreatestFixedPointFunctionApplyMethod); var applyMethodGenericMethod = applyMethodGenericClass.MakeGenericMethod(anaClassGenericParameters[0]); var applyBody = apply.GetILGenerator(); applyBody.Emit(OpCodes.Ldarg_1); applyBody.Emit(OpCodes.Ldarg_0); applyBody.Emit(OpCodes.Ldfld, seedField); applyBody.Emit(OpCodes.Ldarg_0); applyBody.Emit(OpCodes.Ldfld, generatorField); applyBody.Emit(OpCodes.Callvirt, applyMethodGenericMethod); applyBody.Emit(OpCodes.Ret); }
private static GenericTypeParameterBuilder[] BuildGenericParameters(DynamicTypeInfo info, TypeBuilder typBuilder) { // generates the names T0, T1, T2 ... String[] parameterNames = Enumerable.Range(0, info.PropertyNames.Count).Select(i => "T" + i.ToString()).ToArray(); var parameterBuilders = typBuilder.DefineGenericParameters(parameterNames); return parameterBuilders; }
private void CompileOutFunction() { OutFunction = utilityClass.DefineNestedType(Constants.OutFunctionClassName, TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic); var genericParameters = declaration.TypeParameters.Any() ? OutFunction.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; var fGreatestFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, TypeBuilder, genericParameters, runtimeContainer); OutFunction.AddInterfaceImplementation(typeof(IFunction<,>).MakeGenericType(fGreatestFixedPoint, TypeBuilder)); OutFunctionConstructor = OutFunction.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, TypeBuilder.EmptyTypes); var outCtorBody = OutFunctionConstructor.GetILGenerator(); outCtorBody.Emit(OpCodes.Ldarg_0); outCtorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]); outCtorBody.Emit(OpCodes.Ret); var call = OutFunction.DefineMethod(Constants.CallMethodName, MethodAttributes.Public | MethodAttributes.Virtual, TypeBuilder, new Type[] { fGreatestFixedPoint }); var callBody = call.GetILGenerator(); callBody.Emit(OpCodes.Ldarg_1); callBody.Emit(OpCodes.Call, declaration.TypeParameters.Any() ? Out.MakeGenericMethod(genericParameters) : Out); callBody.Emit(OpCodes.Ret); }