Skip to content

Commit 43a8e8b

Browse files
committed
增加动态创建实体API
1 parent 91da92b commit 43a8e8b

File tree

3 files changed

+73
-18
lines changed

3 files changed

+73
-18
lines changed

FreeSql/Extensions/CodeFirstExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using FreeSql.DataAnnotations;
45
using FreeSql.Internal;
56

67
namespace FreeSql.Extensions
@@ -14,9 +15,9 @@ public static class CodeFirstExtensions
1415
/// 动态创建Class Type
1516
/// </summary>
1617
/// <returns></returns>
17-
public static DynamicCompileBuilder DynamicBuilder(this ICodeFirst codeFirst)
18+
public static DynamicCompileBuilder DynamicEntity(this ICodeFirst codeFirst, string className, TableAttribute tableAttribute)
1819
{
19-
return new DynamicCompileBuilder();
20+
return new DynamicCompileBuilder().SetClass(className, tableAttribute);
2021
}
2122

2223
/// <summary>
@@ -25,7 +26,7 @@ public static DynamicCompileBuilder DynamicBuilder(this ICodeFirst codeFirst)
2526
/// <param name="type"></param>
2627
/// <param name="porpertys"></param>
2728
/// <returns></returns>
28-
public static object CreateObjectByType(this ICodeFirst codeFirst, Type type,
29+
public static object CreateDynamicEntityInstance(this Type type,
2930
Dictionary<string, object> porpertys)
3031
{
3132
return DynamicCompileBuilder.CreateObjectByType(type, porpertys);

FreeSql/FreeSql.xml

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Internal/DynamicCompileBuilder.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace 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

Comments
 (0)