private static string GenerateCode(this IRecordDescriptor descriptor, string language, string className, string @namespace) { var codeNamespace = new CodeNamespace(@namespace); codeNamespace.Imports.Add(new CodeNamespaceImport("System")); codeNamespace.GenerateClass(descriptor, className, out bool hasArrayType); if (hasArrayType) { codeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic")); } var targetUnit = new CodeCompileUnit(); targetUnit.Namespaces.Add(codeNamespace); var provider = CodeDomProvider.CreateProvider(language); var options = new CodeGeneratorOptions { BracingStyle = "C", BlankLinesBetweenMembers = true }; var sb = new StringBuilder(); using (var writer = new StringWriterWithEncoding(sb, Encoding.UTF8)) { provider.GenerateCodeFromCompileUnit(targetUnit, writer, options); } return(sb.ToString()); }
private static void AddProperties(this CodeTypeDeclaration targetClass, CodeNamespace codeNamespace, IRecordDescriptor descriptor, out bool hasArrayType) { hasArrayType = false; foreach (var field in descriptor.Fields) { if (field.Value.IsArray) { codeNamespace.GenerateClass((IRecordDescriptor)field.Value, $"{field.Key}Item", out hasArrayType); targetClass.AddArrayProperty(field.Key, $"{field.Key}Item"); hasArrayType = true; continue; } targetClass.AddProperty(field.Key, field.Value); } }