Beispiel #1
0
        private void GenerateMethods(LambdaExpression lambda)
        {
            var stringWriter = new StringWriter();
            var formatter    = new TextFormatter(stringWriter);
            var writer       = new CSharpWriter(formatter, null);

            var usings = ExpressionSearcher.Search(lambda, e => true).Select(e => e.Type.Namespace).Distinct().ToList();

            var ordered = usings.OrderBy(s => s).OrderBy(s => !s.StartsWith("System")).ToList();

            foreach (var @using in ordered)
            {
                formatter.Write("using");
                formatter.WriteSpace();
                formatter.Write(@using);
                formatter.Write(";");
                formatter.WriteLine();
            }

            formatter.WriteLine();

            formatter.Write("namespace");
            formatter.WriteSpace();
            formatter.Write(name);
            formatter.WriteLine();

            writer.VisitBlock(() =>
            {
                formatter.Write($"public class {typeBuilder.Name}");
                formatter.WriteLine();

                writer.VisitBlock(() =>
                {
                    WriteLambdas(lambda, formatter);
                });

                formatter.WriteLine();
            });

            File.WriteAllText(symbolDocument.FileName, stringWriter.ToString());
        }