@@ -18,20 +18,19 @@ namespace FreeSql.Internal
1818 public class DynamicCompileBuilder
1919 {
2020 private string _className = string . Empty ;
21- private TableAttribute _tableAttribute = null ;
21+ private Attribute [ ] _tableAttributes = null ;
2222 private List < DynamicPropertyInfo > _properties = new List < DynamicPropertyInfo > ( ) ;
2323
2424 /// <summary>
2525 /// 配置Class
2626 /// </summary>
2727 /// <param name="className">类名</param>
28- /// <param name="tableAttribute ">类标记的特性[Table(Name = "xxx")]</param>
28+ /// <param name="attributes ">类标记的特性[Table(Name = "xxx")]</param>
2929 /// <returns></returns>
30- public DynamicCompileBuilder SetClass ( string className , TableAttribute tableAttribute )
30+ public DynamicCompileBuilder ( string className , params Attribute [ ] attributes )
3131 {
3232 _className = className ;
33- _tableAttribute = tableAttribute ;
34- return this ;
33+ _tableAttributes = attributes ;
3534 }
3635
3736 /// <summary>
@@ -54,22 +53,21 @@ public DynamicCompileBuilder Property(string propertyName, Type propertyType, pa
5453
5554 private void SetTableAttribute ( ref TypeBuilder typeBuilder )
5655 {
57- var classCtorInfo = typeof ( TableAttribute ) . GetConstructor ( new Type [ ] { } ) ;
58- var propertyInfos = typeof ( TableAttribute ) . GetProperties ( ) . Where ( p => p . CanWrite == true ) . ToArray ( ) ;
59- if ( _tableAttribute == null )
60- {
61- return ;
62- }
56+ if ( _tableAttributes == null ) return ;
6357
6458 var propertyValues = new ArrayList ( ) ;
65- foreach ( var propertyInfo in _tableAttribute . GetType ( ) . GetProperties ( ) . Where ( p => p . CanWrite == true ) )
59+ foreach ( var tableAttribute in _tableAttributes )
6660 {
67- propertyValues . Add ( propertyInfo . GetValue ( _tableAttribute ) ) ;
68- }
61+ if ( tableAttribute == null ) continue ;
6962
70- var customAttributeBuilder =
71- new CustomAttributeBuilder ( classCtorInfo , new object [ 0 ] , propertyInfos , propertyValues . ToArray ( ) ) ;
72- typeBuilder . SetCustomAttribute ( customAttributeBuilder ) ;
63+ var classCtorInfo = tableAttribute . GetType ( ) . GetConstructor ( new Type [ ] { } ) ;
64+ var propertyInfos = tableAttribute . GetType ( ) . GetProperties ( ) . Where ( p => p . CanWrite == true ) . ToArray ( ) ;
65+ foreach ( var propertyInfo in propertyInfos )
66+ propertyValues . Add ( propertyInfo . GetValue ( tableAttribute ) ) ;
67+
68+ var customAttributeBuilder = new CustomAttributeBuilder ( classCtorInfo , new object [ 0 ] , propertyInfos , propertyValues . ToArray ( ) ) ;
69+ typeBuilder . SetCustomAttribute ( customAttributeBuilder ) ;
70+ }
7371 }
7472
7573 private void SetPropertys ( ref TypeBuilder typeBuilder )
@@ -100,8 +98,7 @@ private void SetPropertys(ref TypeBuilder typeBuilder)
10098 ilOfSet . Emit ( OpCodes . Ret ) ;
10199
102100 //设置属性
103- var propertyBuilder =
104- typeBuilder . DefineProperty ( propertyName , PropertyAttributes . None , propertyType , null ) ;
101+ var propertyBuilder = typeBuilder . DefineProperty ( propertyName , PropertyAttributes . None , propertyType , null ) ;
105102 propertyBuilder . SetGetMethod ( methodGet ) ;
106103 propertyBuilder . SetSetMethod ( methodSet ) ;
107104
@@ -115,19 +112,15 @@ private void SetPropertys(ref TypeBuilder typeBuilder)
115112
116113 private void SetPropertyAttribute < T > ( ref PropertyBuilder propertyBuilder , T tAttribute )
117114 {
118- if ( tAttribute == null )
119- return ;
115+ if ( tAttribute == null ) return ;
120116
117+ var propertyInfos = tAttribute . GetType ( ) . GetProperties ( ) . Where ( p => p . CanWrite == true ) . ToArray ( ) ;
118+ var constructor = tAttribute . GetType ( ) . GetConstructor ( new Type [ ] { } ) ;
121119 var propertyValues = new ArrayList ( ) ;
122- foreach ( var propertyInfo in tAttribute . GetType ( ) . GetProperties ( ) . Where ( p => p . CanWrite == true ) )
123- {
120+ foreach ( var propertyInfo in propertyInfos )
124121 propertyValues . Add ( propertyInfo . GetValue ( tAttribute ) ) ;
125- }
126122
127- var propertyInfos = tAttribute . GetType ( ) . GetProperties ( ) . Where ( p => p . CanWrite == true ) . ToArray ( ) ;
128- var constructor = tAttribute . GetType ( ) . GetConstructor ( new Type [ ] { } ) ;
129- var customAttributeBuilder =
130- new CustomAttributeBuilder ( constructor , new object [ 0 ] , propertyInfos , propertyValues . ToArray ( ) ) ;
123+ var customAttributeBuilder = new CustomAttributeBuilder ( constructor , new object [ 0 ] , propertyInfos , propertyValues . ToArray ( ) ) ;
131124 propertyBuilder . SetCustomAttribute ( customAttributeBuilder ) ;
132125 }
133126
@@ -259,12 +252,13 @@ private static string Md5Encryption(string inputStr)
259252
260253 return result ;
261254 }
255+
256+ class DynamicPropertyInfo
257+ {
258+ public string PropertyName { get ; set ; } = string . Empty ;
259+ public Type PropertyType { get ; set ; }
260+ public Attribute [ ] Attributes { get ; set ; }
261+ }
262262 }
263263#endif
264- internal class DynamicPropertyInfo
265- {
266- public string PropertyName { get ; set ; } = string . Empty ;
267- public Type PropertyType { get ; set ; }
268- public Attribute [ ] Attributes { get ; set ; }
269- }
270264}
0 commit comments