internal void DefaultCtorInjector(string typeDefVar, BaseTypeDeclarationSyntax declaringClass) { var ctorMethodDefinitionExp = CecilDefinitionsFactory.Constructor(Context, out var ctorLocalVar, declaringClass.Identifier.ValueText, DefaultCtorAccessibilityFor(declaringClass), Array.Empty <string>()); AddCecilExpression(ctorMethodDefinitionExp); AddCecilExpression($"{typeDefVar}.Methods.Add({ctorLocalVar});"); var ctorBodyIL = TempLocalVar("il"); AddCecilExpression($@"var {ctorBodyIL} = {ctorLocalVar}.Body.GetILProcessor();"); AddCilInstruction(ctorBodyIL, OpCodes.Ldarg_0); AddCilInstruction(ctorBodyIL, OpCodes.Call, ResolveDefaultCtorFor(typeDefVar, declaringClass)); AddCilInstruction(ctorBodyIL, OpCodes.Ret); Context[ctorLocalVar] = ""; }
public override void VisitDelegateDeclaration(DelegateDeclarationSyntax node) { var typeVar = LocalVariableNameForId(NextLocalVariableTypeId()); var accessibility = ModifiersToCecil("TypeAttributes", node.Modifiers, "Private"); var typeDef = CecilDefinitionsFactory.Type( Context, typeVar, node.Identifier.ValueText, DefaultTypeAttributeFor(node).AppendModifier(accessibility), Context.TypeResolver.Resolve("System.MulticastDelegate"), false, Array.Empty <string>(), node.TypeParameterList, "IsAnsiClass = true"); AddCecilExpressions(typeDef); HandleAttributesInMemberDeclaration(node.AttributeLists, typeVar); using (Context.DefinitionVariables.WithCurrent("", node.Identifier.ValueText, MemberKind.Type, typeVar)) { // Delegate ctor AddCecilExpression(CecilDefinitionsFactory.Constructor(Context, out var ctorLocalVar, node.Identifier.Text, "MethodAttributes.FamANDAssem | MethodAttributes.Family", new[] { "System.Object", "System.IntPtr" }, "IsRuntime = true")); AddCecilExpression($"{ctorLocalVar}.Parameters.Add(new ParameterDefinition({Context.TypeResolver.ResolvePredefinedType("Object")}));"); AddCecilExpression($"{ctorLocalVar}.Parameters.Add(new ParameterDefinition({Context.TypeResolver.ResolvePredefinedType("IntPtr")}));"); AddCecilExpression($"{typeVar}.Methods.Add({ctorLocalVar});"); AddDelegateMethod(typeVar, "Invoke", ResolveType(node.ReturnType), node.ParameterList.Parameters, (methodVar, param) => CecilDefinitionsFactory.Parameter(param, Context.SemanticModel, methodVar, TempLocalVar($"{param.Identifier.ValueText}"), ResolveType(param.Type))); // BeginInvoke() method var beginInvokeMethodVar = TempLocalVar("beginInvoke"); AddCecilExpression( $@"var {beginInvokeMethodVar} = new MethodDefinition(""BeginInvoke"", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual, {Context.TypeResolver.Resolve("System.IAsyncResult")}) {{ HasThis = true, IsRuntime = true, }};" ); foreach (var param in node.ParameterList.Parameters) { var paramExps = CecilDefinitionsFactory.Parameter(param, Context.SemanticModel, beginInvokeMethodVar, TempLocalVar($"{param.Identifier.ValueText}"), ResolveType(param.Type)); AddCecilExpressions(paramExps); } AddCecilExpression($"{beginInvokeMethodVar}.Parameters.Add(new ParameterDefinition({Context.TypeResolver.Resolve("System.AsyncCallback")}));"); AddCecilExpression($"{beginInvokeMethodVar}.Parameters.Add(new ParameterDefinition({Context.TypeResolver.ResolvePredefinedType("Object")}));"); AddCecilExpression($"{typeVar}.Methods.Add({beginInvokeMethodVar});"); AddDelegateMethod(typeVar, "EndInvoke", ResolveType(node.ReturnType), node.ParameterList.Parameters, (methodVar, param) => CecilDefinitionsFactory.Parameter(param, Context.SemanticModel, methodVar, TempLocalVar("ar"), Context.TypeResolver.Resolve("System.IAsyncResult"))); base.VisitDelegateDeclaration(node); } void AddDelegateMethod(string typeLocalVar, string methodName, string returnTypeName, in SeparatedSyntaxList <ParameterSyntax> parameters, Func <string, ParameterSyntax, IEnumerable <string> > paramterHandler) { var methodLocalVar = TempLocalVar(methodName.ToLower()); AddCecilExpression( $@"var {methodLocalVar} = new MethodDefinition(""{methodName}"", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual, {returnTypeName}) {{ HasThis = true, IsRuntime = true, }};" ); foreach (var param in parameters) { AddCecilExpressions(paramterHandler(methodLocalVar, param)); } AddCecilExpression($"{typeLocalVar}.Methods.Add({methodLocalVar});"); } }