private void SetResult(UserOperatorInterpreterContext context) { var collectionFormat = formates.FirstOrDefault(p => p.IsCollection); StringBuilder sbFormat = new StringBuilder(); foreach (var item in formates) { if (item.IsCollection) { sbFormat.Append("{0}"); } else { sbFormat.Append(item.FormatValue.FirstOrDefault().Replace("{", "{{").Replace("}", "}}")); } } if (collectionFormat == null) { context.Result.Add(string.Format(sbFormat.ToString())); } else { foreach (var item in collectionFormat.FormatValue) { string result = string.Format(sbFormat.ToString(), item); context.Result.Add(result); } } }
public void Interpreter(UserOperatorInterpreterContext context) { formates = new List <Format>(); SetFormats(context); SetFormatValue(); SetResult(context); }
private void SetFormats(UserOperatorInterpreterContext context) { string format = context.Format; int collectionBeginIndex = format.IndexOf('['); int collectionEndIndex = format.IndexOf(']'); if (collectionBeginIndex > 0 && collectionBeginIndex < collectionEndIndex) { formates.Add(new Format { Key = format.Substring(0, collectionBeginIndex), Data = context.Data, IsCollection = false }); string temp = format.Substring(collectionBeginIndex + 1, collectionEndIndex - collectionBeginIndex - 1); var collectionFormat = temp.Split(new string[] { "=>" }, StringSplitOptions.RemoveEmptyEntries); if (collectionFormat.Length != 2) { throw new Exception("明细格式不正确!"); } formates.Add(new Format { Key = collectionFormat[1].Trim(), Data = GetObjectValue(context.Data, collectionFormat[0].Trim()), IsCollection = true }); if (collectionEndIndex < format.Length - 1) { formates.Add(new Format { Key = format.Substring(collectionEndIndex + 1, format.Length - (collectionEndIndex + 1)), Data = context.Data, IsCollection = false }); } } else { formates.Add(new Format { Key = format, Data = context.Data, IsCollection = false }); } }