예제 #1
0
        private static void ToCode(this CSharp.Class @class, CsBuilder builder)
        {
            builder.StartClass(@class.AccessModifiers, @class.ClassModifiers, @class.Keyword, @class.Name);

            foreach (var field in @class.Fields)
            {
                builder.StartField(field.AccessModifiers, field.FieldModifiers, field.TypeName, field.Name);

                var initExpr = field.InitExpression;
                if (!String.IsNullOrEmpty(initExpr))
                {
                    builder.Append(initExpr);
                }
            }

            foreach (var property in @class.Properties)
            {
                builder.Property(property.AccessModifiers, property.TypeName, property.Name);
            }

            foreach (var method in @class.Methods)
            {
                var parameters = method.Parameters
                                 .Select(p => (name: p.Name, type: p.TypeName))
                                 .ToArray();
                builder.WriteIndent();
                builder.StartMethod(method.AccessModifiers, method.MethodModifiers, method.TypeName, method.Name, parameters);
                builder.WriteIndent();
                builder.AppendLine(method.GetBody(0).ToString());
                builder.EndScope();
            }

            foreach (var @enum in @class.Enums)
            {
                builder.StartEnum(@enum.AccessModifiers, @enum.Name, @enum.BaseTypeName);

                foreach (var option in @enum.Options)
                {
                    builder.WriteIndent();
                    builder.Append($"{option.Name}");
                    if (!String.IsNullOrEmpty(option.Value))
                    {
                        builder.Append($" = {option.Value}");
                    }
                    builder.AppendLine(",");
                }

                builder.EndScope();
            }

            foreach (var nested in @class.Classes)
            {
                nested.ToCode(builder);
            }

            builder.EndScope();
        }
예제 #2
0
        public static string ToCode(this CSharp.Namespace ns)
        {
            var builder = new CsBuilder();

            builder.StartNamespace(ns.Name);

            foreach (var @using in ns.Usings)
            {
                builder.Using(@using);
            }

            foreach (var @class in ns.Classes)
            {
                @class.ToCode(builder);
            }

            builder.EndScope();
            return(builder.ToString());
        }