DynamicProperyBuilder.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection.Emit;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Runtime.InteropServices;
  9. namespace SqlSugar
  10. {
  11. public class DynamicProperyBuilder
  12. {
  13. private bool IsCache = false;
  14. public static DynamicProperyBuilder CopyNew()
  15. {
  16. return new DynamicProperyBuilder();
  17. }
  18. public DynamicBuilder baseBuilder;
  19. public DynamicProperyBuilder CreateProperty(string propertyName, Type properyType, SugarColumn column=null,bool isSplitField=false, Navigate navigate=null)
  20. {
  21. if (column == null)
  22. {
  23. column = new SugarColumn()
  24. {
  25. ColumnName=propertyName
  26. };
  27. }
  28. PropertyMetadata addItem = new PropertyMetadata();
  29. addItem.Name = propertyName;
  30. addItem.Type = properyType;
  31. addItem.CustomAttributes = new List<CustomAttributeBuilder>() { baseBuilder.GetProperty(column) };
  32. if (navigate != null)
  33. {
  34. addItem.CustomAttributes.Add(BuildNavigateAttribute(navigate));
  35. }
  36. baseBuilder.propertyAttr.Add(addItem);
  37. if (isSplitField)
  38. {
  39. addItem.CustomAttributes.Add(baseBuilder.GetSplitFieldAttr(new SplitFieldAttribute()));
  40. }
  41. return this;
  42. }
  43. public DynamicProperyBuilder WithCache(bool isCache=true)
  44. {
  45. IsCache = isCache;
  46. return this;
  47. }
  48. public Type BuilderType()
  49. {
  50. if (IsCache)
  51. {
  52. var key = baseBuilder.entityName + string.Join("_", baseBuilder.propertyAttr.Select(it => it.Name + it.Type.Name));
  53. return new ReflectionInoCacheService().GetOrCreate(key,() =>
  54. {
  55. var result = DynamicBuilderHelper.CreateDynamicClass(baseBuilder.entityName, baseBuilder.propertyAttr, TypeAttributes.Public, baseBuilder.entityAttr, baseBuilder.baseType, baseBuilder.interfaces);
  56. return result;
  57. });
  58. }
  59. else
  60. {
  61. var result = DynamicBuilderHelper.CreateDynamicClass(baseBuilder.entityName, baseBuilder.propertyAttr, TypeAttributes.Public, baseBuilder.entityAttr, baseBuilder.baseType, baseBuilder.interfaces);
  62. return result;
  63. }
  64. }
  65. public CustomAttributeBuilder BuildNavigateAttribute(Navigate navigate)
  66. {
  67. NavigateType navigatType = navigate.NavigatType;
  68. string name = navigate.Name;
  69. string name2 = navigate.Name2;
  70. string whereSql = navigate.WhereSql;
  71. Type mappingTableType = navigate.MappingType;
  72. string typeAiD = navigate.MappingAId;
  73. string typeBId = navigate.MappingBId;
  74. ConstructorInfo constructor;
  75. object[] constructorArgs;
  76. if (mappingTableType != null && typeAiD != null && typeBId != null)
  77. {
  78. constructor = typeof(Navigate).GetConstructor(new Type[] { typeof(Type), typeof(string), typeof(string), typeof(string) });
  79. constructorArgs = new object[] { mappingTableType, typeAiD, typeBId, whereSql };
  80. }
  81. else if (!string.IsNullOrEmpty(whereSql))
  82. {
  83. constructor = typeof(Navigate).GetConstructor(new Type[] { typeof(NavigateType), typeof(string), typeof(string), typeof(string) });
  84. constructorArgs = new object[] { navigatType, name, name2, whereSql };
  85. }
  86. else if (!string.IsNullOrEmpty(name2))
  87. {
  88. constructor = typeof(Navigate).GetConstructor(new Type[] { typeof(NavigateType), typeof(string), typeof(string) });
  89. constructorArgs = new object[] { navigatType, name, name2 };
  90. }
  91. else
  92. {
  93. constructor = typeof(Navigate).GetConstructor(new Type[] { typeof(NavigateType), typeof(string) });
  94. constructorArgs = new object[] { navigatType, name };
  95. }
  96. return new CustomAttributeBuilder(constructor, constructorArgs);
  97. }
  98. }
  99. }