private TryCatchCode GetTryCatch(XElement element) { var result = new TryCatchCode(); XElement elementTry = element.Child("TryCatch.Try"); result.Try = Parse(elementTry.Elements().FirstOrDefault())?.WrapInSequence(); result.Catches = new List <CatchCode>(); IEnumerable <XElement> catchElements = element.Child("TryCatch.Catches").Children("Catch"); foreach (var catchElement in catchElements) { XElement activityAction = catchElement.Child("ActivityAction"); Sequence body = Parse(activityAction.Elements().Last())?.WrapInSequence(); XElement argument = activityAction.Child("ActivityAction.Argument").Child("DelegateInArgument"); string exceptionType = argument.GetAttribute("TypeArguments").Value; if (exceptionType.Contains(":")) { exceptionType = exceptionType.Split(':').Last(); } string exceptionVariableName = argument.GetAttribute("Name").Value; result.Catches.Add(new CatchCode { Body = body, Exception = $"{exceptionType} {exceptionVariableName}" }); } return(result); }
private void WriteTryCatch(StringWriter writer, TryCatchCode tryCatchCode, int tabs) { writer.WriteLineTabs("try", tabs); WriteConstruction(writer, tryCatchCode.Try, tabs + 1); foreach (CatchCode catchCode in tryCatchCode.Catches) { writer.WriteLineTabs($"catch ({catchCode.Exception})", tabs); WriteConstruction(writer, catchCode.Body, tabs + 1); } if (tryCatchCode.Finally != null) { writer.WriteLineTabs($"finally", tabs); WriteConstruction(writer, tryCatchCode.Finally, tabs + 1); } }
private Method CreateMethodFromCustomActivity(CustomActivityCode entity) { var result = new Method { AccessModifier = "private", Name = entity.Name, ReturnType = entity.ReturnType, Parameters = new List <MethodParameter>(), Sequence = new Sequence() }; var objectName = "entity"; result.Sequence.Values.Add(new AssignCode { To = $"var {objectName}", Value = $"new {entity.Name}()" }); var finallyBlock = new Sequence(); foreach (CustomMethodAssignCode assign in entity.Assigns) { result.Parameters.AddRange(fieldManager.GetUsingVariables(assign.Value) .Select(x => new MethodParameter { Variable = x, Direction = assign.Direction })); result.Sequence.Values.Add(new AssignCode { To = $"{objectName}.{assign.To}", Value = assign.Value }); if (assign.Direction == ParameterDirection.InOut || assign.Direction == ParameterDirection.Out) { finallyBlock.Values.Add(new AssignCode { To = assign.Value, Value = $"{objectName}.{assign.To}" }); } } for (var i = result.Parameters.Count - 1; i >= 0; i--) { MethodParameter parameter = result.Parameters[i]; if (result.Parameters.Where(x => x.Variable.Name == parameter.Variable.Name).Count() > 1) { result.Parameters.RemoveAt(i); } } var executeLine = $"{objectName}.Execute()"; if (entity.ReturnType != "void") { executeLine = "return " + executeLine; } var executeStringCode = new StringCode { Value = executeLine }; if (finallyBlock.Values.Count > 0) { var tryFinally = new TryCatchCode { Try = new StringCode { Value = executeLine }.WrapInSequence(), Catches = new List <CatchCode>(), Finally = finallyBlock }; result.Sequence.Values.Add(tryFinally); } else { result.Sequence.Values.Add(executeStringCode); } entity.Method = result; return(result); }