public SetReturnType ( System returnType ) : void | ||
returnType | System | |
Résultat | void |
/// <summary> /// Copies the method signature from one method to another. /// This includes generic parameters, constraints and parameters. /// </summary> /// <param name="sourceMethod">The source method.</param> /// <param name="targetMethod">The target method.</param> internal static void CopyMethodSignature(MethodInfo sourceMethod, MethodBuilder targetMethod) { CopyGenericSignature(sourceMethod, targetMethod); targetMethod.SetReturnType(sourceMethod.ReturnType); // copy the parameters and attributes // it seems that we can use the source parameters directly because the target method is derived // from the source method var parameters = sourceMethod.GetParameters(); targetMethod.SetParameters(parameters.Select(p => p.ParameterType).ToArray()); for (int i = 0; i < parameters.Length; i++) { var parameter = parameters[i]; targetMethod.DefineParameter(i + 1, parameter.Attributes, parameter.Name); } }
private static void AdjustParameterTypes(ParameterInfo[] parameters, Func <Type, Type> typeReplacer, MethodDefinition method) { var fixedParams = new Type[parameters.Length]; foreach (var parameter in parameters) { var paramType = typeReplacer(parameter.ParameterType); fixedParams[parameter.Position] = paramType; method.DefineParameter(parameter.Position, parameter.Attributes, parameter.Name); } // Set fixed parameters having correct generic parameters. method.SetParameters(fixedParams); // Set fixed return type with correct generic parameter. method.SetReturnType(typeReplacer(method.ReturnType)); }
/// <summary> /// Initializes a new instance of the <see cref="MethodBuilderHelper"/> class /// with the specified parameters. /// </summary> /// <param name="typeBuilder">Associated <see cref="TypeBuilderHelper"/>.</param> /// <param name="methodBuilder">A <see cref="MethodBuilder"/></param> /// <param name="genericArguments">Generic arguments of the method.</param> /// <param name="returnType">The return type of the method.</param> /// <param name="parameterTypes">The types of the parameters of the method.</param> internal MethodBuilderHelper( TypeBuilderHelper typeBuilder, MethodBuilder methodBuilder, Type[] genericArguments, Type returnType, Type[] parameterTypes ) : base(typeBuilder) { if (methodBuilder == null) throw new ArgumentNullException("methodBuilder"); if (genericArguments == null) throw new ArgumentNullException("genericArguments"); _methodBuilder = methodBuilder; var genArgNames = genericArguments.Select(t => t.Name).ToArray(); var genParams = methodBuilder.DefineGenericParameters(genArgNames); // Copy parameter constraints. // List<Type> interfaceConstraints = null; for (var i = 0; i < genParams.Length; i++) { genParams[i].SetGenericParameterAttributes(genericArguments[i].GenericParameterAttributes); foreach (var constraint in genericArguments[i].GetGenericParameterConstraints()) { if (constraint.IsClass) genParams[i].SetBaseTypeConstraint(constraint); else { if (interfaceConstraints == null) interfaceConstraints = new List<Type>(); interfaceConstraints.Add(constraint); } } if (interfaceConstraints != null && interfaceConstraints.Count != 0) { genParams[i].SetInterfaceConstraints(interfaceConstraints.ToArray()); interfaceConstraints.Clear(); } } // When a method contains a generic parameter we need to replace all // generic types from methodInfoDeclaration with local ones. // for (var i = 0; i < parameterTypes.Length; i++) parameterTypes[i] = TypeHelper.TranslateGenericParameters(parameterTypes[i], genParams); methodBuilder.SetParameters(parameterTypes); methodBuilder.SetReturnType(TypeHelper.TranslateGenericParameters(returnType, genParams)); // Once all generic stuff is done is it is safe to call SetCustomAttribute // methodBuilder.SetCustomAttribute(Type.Assembly.BLToolkitAttribute); }
private void InitializeReturnType(MethodBuilder methodBuilder, MethodReturnType returnType) { methodBuilder.SetReturnType(ResolveReturnType(returnType)); ParameterBuilder parameterBuilder = methodBuilder.DefineParameter(0, System.Reflection.ParameterAttributes.Retval, null); if (returnType.HasConstant) parameterBuilder.SetConstant(returnType.Constant); metadataPass.Add(delegate { InitializeCustomAttributes(parameterBuilder.SetCustomAttribute, returnType.CustomAttributes); }); }
private void CompileOut() { Out = utilityClass.DefineMethod(Constants.OutMethodName, MethodAttributes.Public | MethodAttributes.Static); var genericParameters = declaration.TypeParameters.Any() ? Out.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; var returnType = declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(genericParameters) : TypeBuilder; var fGreatestFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, returnType, genericParameters, runtimeContainer); Out.SetParameters(fGreatestFixedPoint); Out.SetReturnType(returnType); Out.SetCustomAttribute(new CustomAttributeBuilder(typeof(ExtensionAttribute).GetConstructors()[0], new object[0])); var outBody = Out.GetILGenerator(); outBody.Emit(OpCodes.Ldarg_0); outBody.Emit(OpCodes.Newobj, declaration.TypeParameters.Any() ? TypeBuilder.GetConstructor( InFunction.MakeGenericType(genericParameters), InFunctionConstructor) : InFunctionConstructor); outBody.Emit(OpCodes.Call, fmap.MakeGenericMethod(new[] { returnType, fGreatestFixedPoint }.Concat(genericParameters).ToArray())); outBody.Emit(OpCodes.Call, Ana.MakeGenericMethod(new[] { fGreatestFixedPoint }.Concat(genericParameters).ToArray())); outBody.Emit(OpCodes.Ret); }
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 CompileAna() { Ana = utilityClass.DefineMethod(Constants.AnaMethodName, MethodAttributes.Public | MethodAttributes.Static); var anaGenericParameters = Ana.DefineGenericParameters(new[] { Constants.AnaMethodGenericParameterName }.Concat(declaration.TypeParameters).ToArray()); Ana.SetReturnType(declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(anaGenericParameters.Skip(1).ToArray()) : TypeBuilder); var functorClass = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, anaGenericParameters[0], anaGenericParameters.Skip(1).ToArray(), runtimeContainer); var generatorType = typeof(IFunction<,>).MakeGenericType(anaGenericParameters[0], functorClass); Ana.SetParameters(anaGenericParameters[0], generatorType); var anaBody = Ana.GetILGenerator(); anaBody.Emit(OpCodes.Ldarg_0); anaBody.Emit(OpCodes.Ldarg_1); anaBody.Emit(OpCodes.Newobj, TypeBuilder.GetConstructor(AnaClass.MakeGenericType(anaGenericParameters), AnaClassConstructor)); anaBody.Emit(OpCodes.Ret); }
private void CompileIn() { CompileInGeneratingFunction(); In = utilityClass.DefineMethod(Constants.InMethodName, MethodAttributes.Public | MethodAttributes.Static); var genericParameters = declaration.TypeParameters.Any() ? In.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; var resultType = declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(genericParameters.ToArray()) : TypeBuilder; In.SetReturnType(FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, resultType, genericParameters, runtimeContainer)); In.SetParameters(resultType); In.SetCustomAttribute(new CustomAttributeBuilder(typeof(ExtensionAttribute).GetConstructors()[0], new object[0])); var fGreatestFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, resultType, genericParameters, runtimeContainer); var applyMethodGenericClass = declaration.TypeParameters.Any() ? TypeBuilder.GetMethod( TypeBuilder.MakeGenericType(genericParameters), GreatestFixedPointApplyMethod) : GreatestFixedPointApplyMethod; var applyMethodGenericMethod = applyMethodGenericClass.MakeGenericMethod(fGreatestFixedPoint); var inBody = In.GetILGenerator(); inBody.Emit(OpCodes.Ldarg_0); inBody.Emit(OpCodes.Newobj, declaration.TypeParameters.Any() ? TypeBuilder.GetConstructor( InClass.MakeGenericType(genericParameters), InGeneratingFunctionConstructor) : InGeneratingFunctionConstructor); inBody.Emit(OpCodes.Callvirt, applyMethodGenericMethod); inBody.Emit(OpCodes.Ret); }
private static void AdjustParameterTypes(ParameterInfo[] parameters, Func<Type, Type> typeReplacer, MethodDefinition method) { var fixedParams = new Type[parameters.Length]; foreach (var parameter in parameters) { var paramType = typeReplacer(parameter.ParameterType); fixedParams[parameter.Position] = paramType; method.DefineParameter(parameter.Position, parameter.Attributes, parameter.Name); } // Set fixed parameters having correct generic parameters. method.SetParameters(fixedParams); // Set fixed return type with correct generic parameter. method.SetReturnType(typeReplacer(method.ReturnType)); }
public void Compile() { TypeBuilder = module.DefineType(moduleName + '.' + Constants.TypesNamespace + '.' + name, TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract); var genericParameters = declaration.TypeParameters.Any() ? ((TypeBuilder)TypeBuilder).DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; Cata = ((TypeBuilder)TypeBuilder).DefineMethod(Constants.CataMethodName, MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual); var cataReturnType = Cata.DefineGenericParameters(Constants.CataMethodGenericParameterName)[0]; var functorClass = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, cataReturnType, genericParameters, runtimeContainer); Cata.SetParameters(typeof(IFunction<,>).MakeGenericType(functorClass, cataReturnType)); Cata.SetReturnType(cataReturnType); CompileCataFunction(); CompileCataFunction1(); CompileOutClass(); CompileOut(); CompileOutFunction(); CompileIn(); CompileInFunction(); TypeBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(FiniteAttribute).GetConstructors()[0], new object[] { declaration.ConstructorFunctionName, declaration.DestructorFunctionName, declaration.CataFunctionName, InFunction })); }
private void CompileIn() { In = utilityClass.DefineMethod(Constants.InMethodName, MethodAttributes.Public | MethodAttributes.Static); var genericParameters = declaration.TypeParameters.Any() ? In.DefineGenericParameters(declaration.TypeParameters) : TypeBuilder.EmptyTypes; var inputParameter = declaration.TypeParameters.Any() ? TypeBuilder.MakeGenericType(genericParameters.ToArray()) : TypeBuilder; In.SetReturnType(FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, inputParameter, genericParameters, runtimeContainer)); In.SetParameters(inputParameter); In.SetCustomAttribute(new CustomAttributeBuilder(typeof(ExtensionAttribute).GetConstructors()[0], new object[0])); var inBody = In.GetILGenerator(); var fLeastFixedPoint = FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, inputParameter, genericParameters, runtimeContainer); inBody.Emit(OpCodes.Ldarg_0); inBody.Emit(OpCodes.Newobj, declaration.TypeParameters.Any() ? TypeBuilder.GetConstructor( OutFunction.MakeGenericType(genericParameters), OutFunctionConstructor) : OutFunctionConstructor); inBody.Emit(OpCodes.Call, fmap.MakeGenericMethod(new Type[] { fLeastFixedPoint, inputParameter }.Concat(genericParameters).ToArray())); inBody.Emit(OpCodes.Callvirt, declaration.TypeParameters.Any() ? TypeBuilder.GetMethod(inputParameter, Cata).MakeGenericMethod(fLeastFixedPoint) : Cata.MakeGenericMethod(fLeastFixedPoint)); inBody.Emit(OpCodes.Ret); }
protected override void PreBuild() { if (Info != null) { return; } var cont = CurrentContainer; Builder = cont.CreateMethod(Name, Attributes); Info = Builder; if (Generics.Count > 0) { var gb = Builder.DefineGenericParameters(Generics.ToNames()); Generics.RegisterBuilders(gb); } if (ReturnType != null) { Builder.SetReturnType(ReturnType.GainType()); //todo ジェネリクスに対応したTypeを生成する。 } Builder.SetParameters(Arguments.ToTypes()); Arguments.RegisterBuilders(Builder, IsInstance); SpreadGenerator(); }
/// <summary> /// Copies return type attributes to the new type. /// </summary> /// <param name="method"></param> /// <param name="invokeMethod"></param> private static void ProcessReturnParameterAttributes(MethodInfo method, MethodBuilder invokeMethod) { invokeMethod.SetReturnType(method.ReturnType); if (method.ReturnParameter != null) { var returnParameter = invokeMethod.DefineParameter(0, method.ReturnParameter.Attributes, method.ReturnParameter.Name); CopyParameterAttributes(returnParameter, method.ReturnParameter); } }