using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Reflection.Emit; using EntrustSettle.Common.Extensions; using SqlSugar; namespace EntrustSettle.Common.DB.Extension; public static class DynamicBuildException { private static List GetEntityAttr(this DynamicBuilder builder) { FieldInfo fieldInfo = builder.GetType().GetField("entityAttr", BindingFlags.Instance | BindingFlags.NonPublic); List entityAttr = (List) fieldInfo.GetValue(builder); return entityAttr; } private static CustomAttributeBuilder CreateIndex(SugarIndexAttribute indexAttribute) { Type type = typeof(SugarIndexAttribute); var constructorTypes = new List() {typeof(string)}; for (int i = 0; i < indexAttribute.IndexFields.Count; i++) { constructorTypes.AddRange(new[] {typeof(string), typeof(OrderByType)}); } constructorTypes.Add(typeof(bool)); var values = new List() {indexAttribute.IndexName}; foreach (var indexField in indexAttribute.IndexFields) { values.AddRange(new object[] {indexField.Key, indexField.Value}); } values.Add(indexAttribute.IsUnique); return new CustomAttributeBuilder(type.GetConstructor(constructorTypes.ToArray())!, values.ToArray()); } public static DynamicProperyBuilder CreateIndex(this DynamicProperyBuilder builder, SugarIndexAttribute indexAttribute) { var classBuilder = builder.baseBuilder; var entityAttr = classBuilder.GetEntityAttr(); entityAttr.Add(CreateIndex(indexAttribute)); return builder; } }