public override bool CreateMethod(EventDescription eventDescription, string methodName) { CodeMemberMethod method = new CodeMemberMethod(); method.Name = methodName; foreach (EventParameter param in eventDescription.Parameters) { method.Parameters.Add(new CodeParameterDeclarationExpression(param.TypeName, param.Name)); } //Finally, add the new method to the class CodeDomDocDataAdapter adapter = GetDocDataAdapterForPyFile(); adapter.TypeDeclaration.Members.Add(method); adapter.Generate(); return(true); }
public override bool AddEventHandler(EventDescription eventDescription, string objectName, string methodName) { //FixMe: VladD2: Какая-то питоновская чушь! Надо разобраться и переписать. const string Init = "__init__"; //This is not the most optimal solution for WPF since we will call FindLogicalNode for each event handler, //but it simplifies the code generation for now. CodeDomDocDataAdapter adapter = GetDocDataAdapterForNemerleFile(); CodeMemberMethod method = null; //Find the __init__ method foreach (CodeTypeMember ctMember in adapter.TypeDeclaration.Members) { if (ctMember is CodeConstructor) { if (ctMember.Name == Init) { method = ctMember as CodeMemberMethod; break; } } } if (method == null) { method = new CodeConstructor(); method.Name = Init; } //Create a code statement which looks like: LogicalTreeHelper.FindLogicalNode(self.Root, "button1").Click += self.button1_Click var logicalTreeHelper = new CodeTypeReferenceExpression("LogicalTreeHelper"); var findLogicalNodeMethod = new CodeMethodReferenceExpression(logicalTreeHelper, "FindLogicalNode"); var selfWindow = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "Root"); var findLogicalNodeInvoke = new CodeMethodInvokeExpression(findLogicalNodeMethod, selfWindow, new CodeSnippetExpression("\'" + objectName + "\'")); var createDelegateExpression = new CodeDelegateCreateExpression(new CodeTypeReference("System.EventHandler"), new CodeThisReferenceExpression(), methodName); var attachEvent = new CodeAttachEventStatement(findLogicalNodeInvoke, eventDescription.Name, createDelegateExpression); method.Statements.Add(attachEvent); adapter.Generate(); return(true); }