private static List <ClassDefinition> BuildClassDefinitions(BusinessInfo businessInfo, Entity entity, string ns, string formId) { List <ClassDefinition> results = new List <ClassDefinition>(); ClassDefinition definition = new ClassDefinition(); List <ClassPropertyDefinition> classProperties = new List <ClassPropertyDefinition>(entity.DynamicObjectType.Properties.Count); HashSet <string> usingNamespaces = new HashSet <string>(); List <Field> fields = entity.Fields; foreach (DynamicProperty property in entity.DynamicObjectType.Properties) { Field field = fields.FirstOrDefault(u => u.PropertyName.Equals(property.Name)); string usingNamespace = GetNamespaceName(property); if (!string.IsNullOrWhiteSpace(usingNamespace)) { usingNamespaces.Add(usingNamespace); } string annotation = field?.Name; if (string.IsNullOrWhiteSpace(annotation)) { annotation = property.Name; } string type = GetPropertyTypeName(property, field); string name = property.Name; // 基础资料类型属性、单选辅助资料属性 if (property is IComplexProperty complexProperty) { string lookUpObjectId = GetFieldLookUpObjectId(field); type = lookUpObjectId; results.Add(BuildClassDefinition(complexProperty, ns, lookUpObjectId)); } var singlePropertyDefinition = new ClassPropertyDefinition() { Name = name, Annotation = annotation, TypeName = type }; //给字段加上数据表列的特性 if (field != null) { singlePropertyDefinition.Attributes.Add($"[Column(\"{field.FieldName}\")]"); } else { if (property == entity.DynamicObjectType.PrimaryKey) { singlePropertyDefinition.Attributes.Add($"[Column(\"{entity.EntryPkFieldName}\")]"); } else if (property == businessInfo.GetForm().MasterIdDynamicProperty) { singlePropertyDefinition.Attributes.Add($"[Column(\"{businessInfo.GetForm().MasterPKFieldName}\")]"); } else if (entity.SeqDynamicProperty == property) { singlePropertyDefinition.Attributes.Add($"[Column(\"{entity.SeqFieldKey}\")]"); } } classProperties.Add(singlePropertyDefinition); } string className = GetClassName(entity, formId); var classDefinition = definition.SetAnnotation(entity.Name).SetName(className).SetNamespace(ns) .SetProperties(classProperties).SetUsingNamespaces(usingNamespaces); if (!string.IsNullOrWhiteSpace(entity.TableName)) { classDefinition.AddAttribute($"[Table(\"{entity.TableName}\")]"); #warning 需自行定义Table特性所在命名空间(.NET 4.0不提供Table特性定义)ref:https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.schema.tableattribute?view=netframework-4.8&viewFallbackFrom=netframework-4.0 classDefinition.AddUsingNamespaces("Kingdee.Vincent.Core.Domain.Entities"); } results.Add(classDefinition); return(results); }
private static string GenerateClassFile(ClassDefinition definition, string filePath) { if (definition == null) { throw new ArgumentNullException(nameof(definition)); } StringBuilder sb = new StringBuilder(); // 文件头注释 sb.AppendLine("// ================================================================================"); sb.AppendLine("// 描述: 业务对象[" + definition.Name + "]的实体类"); sb.AppendLine("// 此实体类通过代码生成工具自动生成,如需修改,请通过代码生成工具重新生成"); sb.AppendLine("// 生成日期: " + DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒")); sb.AppendLine("// ================================================================================"); // Using foreach (string ns in definition.UsingNamespaces) { sb.AppendLine($"using {ns};"); } sb.AppendLine(); // namespace sb.AppendLine("namespace " + definition.Namespace); sb.AppendLine("{"); // class desc sb.AppendLine(" /// <summary>"); sb.AppendLine($" /// {definition.Annotation}"); sb.AppendLine(" /// </summary>"); // class attributes foreach (string attribute in definition.Attributes) { sb.AppendLine(" " + attribute); } // class sb.AppendLine($" public class {definition.Name}"); sb.AppendLine(" {"); // field foreach (ClassPropertyDefinition property in definition.Properties) { sb.AppendLine(" /// <summary>"); sb.AppendLine($" /// {property.Annotation}"); sb.AppendLine(" /// </summary>"); foreach (string propertyAttribute in property.Attributes) { sb.AppendLine(" " + propertyAttribute); } sb.AppendLine($" public {property.TypeName} {property.Name} {{ get; set; }}"); sb.AppendLine(); } sb.AppendLine(" }"); sb.AppendLine("}"); SaveFile(sb.ToString(), filePath); return(filePath); }