Skip to content

Commit 235a2c2

Browse files
committed
- 动态操作表结构相关的 API
1 parent a0f4c90 commit 235a2c2

File tree

4 files changed

+43
-100
lines changed

4 files changed

+43
-100
lines changed

FreeSql/Extensions/CodeFirstExtensions.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

FreeSql/Extensions/FreeSqlGlobalExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FreeSql;
22
using FreeSql.DataAnnotations;
3+
using FreeSql.Internal;
34
using FreeSql.Internal.CommonProvider;
45
using FreeSql.Internal.Model;
56
using FreeSql.Internal.ObjectPool;
@@ -1299,4 +1300,18 @@ static NativeTuple<Select1Provider<object>, ReadAnonymousTypeAfInfo, string> Sel
12991300
return NativeTuple.Create(query, af, sql);
13001301
}
13011302
#endregion
1303+
1304+
#region DynamicEntity
1305+
#if net40 || NETSTANDARD2_0
1306+
#else
1307+
/// <summary>
1308+
/// 动态构建Class Type
1309+
/// </summary>
1310+
/// <returns></returns>
1311+
public static DynamicCompileBuilder DynamicEntity(this ICodeFirst codeFirst, string className, TableAttribute tableAttribute)
1312+
{
1313+
return new DynamicCompileBuilder(className, tableAttribute);
1314+
}
1315+
#endif
1316+
#endregion
13021317
}

FreeSql/Extensions/TypeExtensions.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

FreeSql/Internal/DynamicCompileBuilder.cs

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)