1111namespace FreeSql . Internal
1212{
1313#if net40 || NETSTANDARD2_0
14-
14+
1515#else
1616 public class DynamicCompileBuilder
1717 {
@@ -37,15 +37,15 @@ public DynamicCompileBuilder SetClass(string className, TableAttribute tableAttr
3737 /// </summary>
3838 /// <param name="propertyName">属性名称</param>
3939 /// <param name="propertyType">属性类型</param>
40- /// <param name="columnAttribute ">属性标记的特性[Column(IsPrimary = true)]</param>
40+ /// <param name="attributes ">属性标记的特性[Column(IsPrimary = true)]</param>
4141 /// <returns></returns>
42- public DynamicCompileBuilder SetProperty ( string propertyName , Type propertyType , ColumnAttribute columnAttribute )
42+ public DynamicCompileBuilder Property ( string propertyName , Type propertyType , params Attribute [ ] attributes )
4343 {
4444 _properties . Add ( new DynamicPropertyInfo ( )
4545 {
4646 PropertyName = propertyName ,
47- PropertyType = propertyType ,
48- ColumnAttribute = columnAttribute
47+ PropertyType = propertyType ,
48+ Attributes = attributes
4949 } ) ;
5050 return this ;
5151 }
@@ -103,24 +103,27 @@ private void SetPropertys(ref TypeBuilder typeBuilder)
103103 propertyBuilder . SetGetMethod ( methodGet ) ;
104104 propertyBuilder . SetSetMethod ( methodSet ) ;
105105
106- //设置特性
107- SetColumnAttribute ( ref propertyBuilder , pinfo ? . ColumnAttribute ) ;
106+ foreach ( var pinfoAttribute in pinfo . Attributes )
107+ {
108+ //设置特性
109+ SetPropertyAttribute ( ref propertyBuilder , pinfoAttribute ) ;
110+ }
108111 }
109112 }
110113
111- private void SetColumnAttribute ( ref PropertyBuilder propertyBuilder , ColumnAttribute columnAttribute = null )
114+ private void SetPropertyAttribute < T > ( ref PropertyBuilder propertyBuilder , T tAttribute )
112115 {
113- if ( columnAttribute == null )
116+ if ( tAttribute == null )
114117 return ;
115118
116119 var propertyValues = new ArrayList ( ) ;
117- foreach ( var propertyInfo in columnAttribute . GetType ( ) . GetProperties ( ) . Where ( p => p . CanWrite == true ) )
120+ foreach ( var propertyInfo in tAttribute . GetType ( ) . GetProperties ( ) . Where ( p => p . CanWrite == true ) )
118121 {
119- propertyValues . Add ( propertyInfo . GetValue ( columnAttribute ) ) ;
122+ propertyValues . Add ( propertyInfo . GetValue ( tAttribute ) ) ;
120123 }
121124
122- var propertyInfos = typeof ( ColumnAttribute ) . GetProperties ( ) . Where ( p => p . CanWrite == true ) . ToArray ( ) ;
123- var constructor = typeof ( ColumnAttribute ) . GetConstructor ( new Type [ ] { } ) ;
125+ var propertyInfos = tAttribute . GetType ( ) . GetProperties ( ) . Where ( p => p . CanWrite == true ) . ToArray ( ) ;
126+ var constructor = tAttribute . GetType ( ) . GetConstructor ( new Type [ ] { } ) ;
124127 var customAttributeBuilder =
125128 new CustomAttributeBuilder ( constructor , new object [ 0 ] , propertyInfos , propertyValues . ToArray ( ) ) ;
126129 propertyBuilder . SetCustomAttribute ( customAttributeBuilder ) ;
@@ -226,8 +229,8 @@ private string FirstCharToUpper(string input)
226229 internal class DynamicPropertyInfo
227230 {
228231 public string PropertyName { get ; set ; } = string . Empty ;
229- public Type PropertyType { get ; set ; } = null ;
230- public ColumnAttribute ColumnAttribute { get ; set ; } = null ;
232+ public Type PropertyType { get ; set ; }
233+ public Attribute [ ] Attributes { get ; set ; }
231234 }
232235}
233236
0 commit comments