mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
Merge pull request #1515 from d4ilys/master
- 基类属性abstract/virtual支持override,支持属性默认值
This commit is contained in:
commit
6adae602cf
@ -5,6 +5,7 @@ using System.Text;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using FreeSql.DataAnnotations;
|
using FreeSql.DataAnnotations;
|
||||||
using FreeSql.Extensions.DynamicEntity;
|
using FreeSql.Extensions.DynamicEntity;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace FreeSql.Tests.DynamicEntity
|
namespace FreeSql.Tests.DynamicEntity
|
||||||
@ -33,12 +34,14 @@ namespace FreeSql.Tests.DynamicEntity
|
|||||||
//根据Type生成表
|
//根据Type生成表
|
||||||
fsql.CodeFirst.SyncStructure(table.Type);
|
fsql.CodeFirst.SyncStructure(table.Type);
|
||||||
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
|
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
|
||||||
|
var objects = fsql.Select<object>().AsType(table.Type).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void AttributeTest()
|
public void AttributeTest()
|
||||||
{
|
{
|
||||||
var table = fsql.CodeFirst.DynamicEntity("AttributeUsers", new TableAttribute() { Name = "T_Attribute_User" },
|
var table = fsql.CodeFirst.DynamicEntity("AttributeUsers",
|
||||||
|
new TableAttribute() { Name = "T_Attribute_User" },
|
||||||
new IndexAttribute("Name_Index1", "Name", false))
|
new IndexAttribute("Name_Index1", "Name", false))
|
||||||
.Property("Id", typeof(int),
|
.Property("Id", typeof(int),
|
||||||
new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 })
|
new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 })
|
||||||
@ -80,20 +83,157 @@ namespace FreeSql.Tests.DynamicEntity
|
|||||||
//根据Type生成表
|
//根据Type生成表
|
||||||
fsql.CodeFirst.SyncStructure(table.Type);
|
fsql.CodeFirst.SyncStructure(table.Type);
|
||||||
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
|
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
|
||||||
}
|
var objects = fsql.Select<object>().AsType(table.Type).ToList();
|
||||||
}
|
|
||||||
public class BaseModel
|
|
||||||
{
|
|
||||||
[Column(Position = 99)]
|
|
||||||
public DateTime UpdateTime
|
|
||||||
{
|
|
||||||
get; set;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Column(Position = 100, StringLength = 20)]
|
[Fact]
|
||||||
public string UpdatePerson
|
public void SuperClassVirtualOverrideTest()
|
||||||
{
|
{
|
||||||
get; set;
|
var table = fsql.CodeFirst.DynamicEntity("Role_VirtualOverride",
|
||||||
|
new TableAttribute() { Name = "T_Role_VirtualOverride" },
|
||||||
|
new IndexAttribute("Name_Index2", "Name", false))
|
||||||
|
.Extend(typeof(BaseModelOverride))
|
||||||
|
.Property("Id", typeof(int),
|
||||||
|
new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 })
|
||||||
|
.Property("Name", typeof(string),
|
||||||
|
new ColumnAttribute() { StringLength = 20, Position = 2 })
|
||||||
|
.Property("Operators", typeof(string), true) //重写 virtual 属性
|
||||||
|
.Build();
|
||||||
|
var dict = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["Name"] = "系统管理员",
|
||||||
|
["UpdateTime"] = DateTime.Now,
|
||||||
|
["UpdatePerson"] = "admin",
|
||||||
|
["Operators"] = "manager"
|
||||||
|
};
|
||||||
|
var instance = table.CreateInstance(dict);
|
||||||
|
//根据Type生成表
|
||||||
|
fsql.CodeFirst.SyncStructure(table.Type);
|
||||||
|
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
|
||||||
|
var objects = fsql.Select<object>().AsType(table.Type).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SuperClassBaseModelAbstractTest()
|
||||||
|
{
|
||||||
|
var table = fsql.CodeFirst.DynamicEntity("Role_AbstractOverride",
|
||||||
|
new TableAttribute() { Name = "T_Role_AbstractOverride" },
|
||||||
|
new IndexAttribute("Name_Index2", "Name", false))
|
||||||
|
.Extend(typeof(BaseModelAbstract))
|
||||||
|
.Property("Id", typeof(int),
|
||||||
|
new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 })
|
||||||
|
.Property("Name", typeof(string),
|
||||||
|
new ColumnAttribute() { StringLength = 20, Position = 2 })
|
||||||
|
.Property("Operators", typeof(string), true) //重写 abstract 属性
|
||||||
|
.Build();
|
||||||
|
var dict = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["Name"] = "系统管理员",
|
||||||
|
["UpdateTime"] = DateTime.Now,
|
||||||
|
["UpdatePerson"] = "admin",
|
||||||
|
["Operators"] = "manager"
|
||||||
|
};
|
||||||
|
var instance = table.CreateInstance(dict);
|
||||||
|
//根据Type生成表
|
||||||
|
fsql.CodeFirst.SyncStructure(table.Type);
|
||||||
|
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
|
||||||
|
var objects = fsql.Select<object>().AsType(table.Type).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SuperClassBaseModelAbstractAndVirtualTest()
|
||||||
|
{
|
||||||
|
var table = fsql.CodeFirst.DynamicEntity("Role_AbstractAndVirtualOverride",
|
||||||
|
new TableAttribute() { Name = "Role_AbstractAndVirtualOverride" },
|
||||||
|
new IndexAttribute("Name_Index2", "Name", false))
|
||||||
|
.Extend(typeof(BaseModelAbstractAndVirtual))
|
||||||
|
.Property("Id", typeof(int),
|
||||||
|
new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 })
|
||||||
|
.Property("Name", typeof(string),
|
||||||
|
new ColumnAttribute() { StringLength = 20, Position = 2 })
|
||||||
|
.Property("Operators", typeof(string), true) //重写 abstract 属性
|
||||||
|
.Property("Operators2", typeof(string), true) //重写 virtual 属性
|
||||||
|
.Build();
|
||||||
|
var dict = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["Name"] = "系统管理员",
|
||||||
|
["UpdateTime"] = DateTime.Now,
|
||||||
|
["UpdatePerson"] = "admin",
|
||||||
|
["Operators"] = "manager",
|
||||||
|
["Operators2"] = "manager2"
|
||||||
|
};
|
||||||
|
var instance = table.CreateInstance(dict);
|
||||||
|
//根据Type生成表
|
||||||
|
fsql.CodeFirst.SyncStructure(table.Type);
|
||||||
|
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
|
||||||
|
var objects = fsql.Select<object>().AsType(table.Type).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DefaultValueTest()
|
||||||
|
{
|
||||||
|
var table = fsql.CodeFirst.DynamicEntity("NormalUsers")
|
||||||
|
.Property("Id", typeof(string))
|
||||||
|
.Property("Age", typeof(int), false, 12)
|
||||||
|
.Property("Longs", typeof(long), false, 16666)
|
||||||
|
.Property("Dates", typeof(DateTime), false, "2023-05-15")
|
||||||
|
.Property("Name", typeof(char), false, '我')
|
||||||
|
.Property("Address", typeof(bool), false, false) //设置默认值
|
||||||
|
.Property("Money", typeof(double), false, 265421.02) //设置默认值
|
||||||
|
.Property("MoneyFloat", typeof(float), false, 26543.02) //设置默认值
|
||||||
|
.Property("MoneyDecimal", typeof(decimal), true, 2663.12560) //设置默认值
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var dict = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["Id"] = Guid.NewGuid().ToString()
|
||||||
|
};
|
||||||
|
var instance = table.CreateInstance(dict);
|
||||||
|
//根据Type生成表
|
||||||
|
fsql.CodeFirst.SyncStructure(table.Type);
|
||||||
|
fsql.Insert<object>().AsType(table.Type).AppendData(instance).ExecuteAffrows();
|
||||||
|
var objects = fsql.Select<object>().AsType(table.Type).ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class BaseModel
|
||||||
|
{
|
||||||
|
[Column(Position = 99)] public DateTime UpdateTime { get; set; }
|
||||||
|
|
||||||
|
[Column(Position = 100, StringLength = 20)]
|
||||||
|
public string UpdatePerson { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BaseModelOverride
|
||||||
|
{
|
||||||
|
[Column(Position = 99)] public DateTime UpdateTime { get; set; }
|
||||||
|
|
||||||
|
[Column(Position = 100, StringLength = 20)]
|
||||||
|
public string UpdatePerson { get; set; }
|
||||||
|
|
||||||
|
public virtual string Operators { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class BaseModelAbstract
|
||||||
|
{
|
||||||
|
[Column(Position = 99)] public DateTime UpdateTime { get; set; }
|
||||||
|
|
||||||
|
[Column(Position = 100, StringLength = 20)]
|
||||||
|
public string UpdatePerson { get; set; }
|
||||||
|
|
||||||
|
public abstract string Operators { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class BaseModelAbstractAndVirtual
|
||||||
|
{
|
||||||
|
[Column(Position = 99)] public DateTime UpdateTime { get; set; }
|
||||||
|
|
||||||
|
[Column(Position = 100, StringLength = 20)]
|
||||||
|
public string UpdatePerson { get; set; }
|
||||||
|
|
||||||
|
public abstract string Operators { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public virtual string Operators2 { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
// by: Daily
|
// by: Daily
|
||||||
|
|
||||||
#if net40 || NETSTANDARD2_0
|
#if net40 || NETSTANDARD2_0
|
||||||
# else
|
#else
|
||||||
|
|
||||||
using FreeSql;
|
using FreeSql;
|
||||||
using FreeSql.DataAnnotations;
|
using FreeSql.DataAnnotations;
|
||||||
@ -11,11 +11,15 @@ using FreeSql.Internal.Model;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Reflection.Emit;
|
using System.Reflection.Emit;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Xml.Serialization;
|
||||||
|
|
||||||
public static class FreeSqlGlobalDynamicEntityExtensions
|
public static class FreeSqlGlobalDynamicEntityExtensions
|
||||||
{
|
{
|
||||||
@ -23,7 +27,8 @@ public static class FreeSqlGlobalDynamicEntityExtensions
|
|||||||
/// 动态构建Class Type
|
/// 动态构建Class Type
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static DynamicCompileBuilder DynamicEntity(this ICodeFirst codeFirst, string className, params Attribute[] attributes)
|
public static DynamicCompileBuilder DynamicEntity(this ICodeFirst codeFirst, string className,
|
||||||
|
params Attribute[] attributes)
|
||||||
{
|
{
|
||||||
return new DynamicCompileBuilder((codeFirst as CodeFirstProvider)._orm, className, attributes);
|
return new DynamicCompileBuilder((codeFirst as CodeFirstProvider)._orm, className, attributes);
|
||||||
}
|
}
|
||||||
@ -38,13 +43,21 @@ public static class FreeSqlGlobalDynamicEntityExtensions
|
|||||||
{
|
{
|
||||||
if (table == null || dict == null) return null;
|
if (table == null || dict == null) return null;
|
||||||
var instance = table.Type.CreateInstanceGetDefaultValue();
|
var instance = table.Type.CreateInstanceGetDefaultValue();
|
||||||
|
//加载默认值
|
||||||
|
var defaultValueInit = table.Type.GetMethod("DefaultValueInit");
|
||||||
|
if (defaultValueInit != null)
|
||||||
|
{
|
||||||
|
defaultValueInit.Invoke(instance, new object[0]);
|
||||||
|
}
|
||||||
foreach (var key in table.ColumnsByCs.Keys)
|
foreach (var key in table.ColumnsByCs.Keys)
|
||||||
{
|
{
|
||||||
if (dict.ContainsKey(key) == false) continue;
|
if (dict.ContainsKey(key) == false) continue;
|
||||||
table.ColumnsByCs[key].SetValue(instance, dict[key]);
|
table.ColumnsByCs[key].SetValue(instance, dict[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 根据实体对象,创建 table 对应的字典
|
/// 根据实体对象,创建 table 对应的字典
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -94,12 +107,59 @@ namespace FreeSql.Extensions.DynamicEntity
|
|||||||
/// <param name="propertyType">属性类型</param>
|
/// <param name="propertyType">属性类型</param>
|
||||||
/// <param name="attributes">属性标记的特性-支持多个</param>
|
/// <param name="attributes">属性标记的特性-支持多个</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public DynamicCompileBuilder Property(string propertyName, Type propertyType, params Attribute[] attributes)
|
public DynamicCompileBuilder Property(string propertyName, Type propertyType,
|
||||||
|
params Attribute[] attributes)
|
||||||
{
|
{
|
||||||
_properties.Add(new DynamicPropertyInfo()
|
_properties.Add(new DynamicPropertyInfo()
|
||||||
{
|
{
|
||||||
PropertyName = propertyName,
|
PropertyName = propertyName,
|
||||||
PropertyType = propertyType,
|
PropertyType = propertyType,
|
||||||
|
DefaultValue = null,
|
||||||
|
Attributes = attributes
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 配置属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="propertyName">属性名称</param>
|
||||||
|
/// <param name="propertyType">属性类型</param>
|
||||||
|
/// <param name="isOverride">该属性是否重写父类属性</param>
|
||||||
|
/// <param name="attributes">属性标记的特性-支持多个</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public DynamicCompileBuilder Property(string propertyName, Type propertyType, bool isOverride,
|
||||||
|
params Attribute[] attributes)
|
||||||
|
{
|
||||||
|
_properties.Add(new DynamicPropertyInfo()
|
||||||
|
{
|
||||||
|
PropertyName = propertyName,
|
||||||
|
PropertyType = propertyType,
|
||||||
|
DefaultValue = null,
|
||||||
|
IsOverride = isOverride,
|
||||||
|
Attributes = attributes
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 配置属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="propertyName">属性名称</param>
|
||||||
|
/// <param name="propertyType">属性类型</param>
|
||||||
|
/// <param name="isOverride">该属性是否重写父类属性</param>
|
||||||
|
/// <param name="defaultValue">属性默认值</param>
|
||||||
|
/// <param name="attributes">属性标记的特性-支持多个</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public DynamicCompileBuilder Property(string propertyName, Type propertyType, bool isOverride,
|
||||||
|
object defaultValue, params Attribute[] attributes)
|
||||||
|
{
|
||||||
|
_properties.Add(new DynamicPropertyInfo()
|
||||||
|
{
|
||||||
|
PropertyName = propertyName,
|
||||||
|
PropertyType = propertyType,
|
||||||
|
DefaultValue = defaultValue,
|
||||||
|
IsOverride = isOverride,
|
||||||
Attributes = attributes
|
Attributes = attributes
|
||||||
});
|
});
|
||||||
return this;
|
return this;
|
||||||
@ -126,7 +186,7 @@ namespace FreeSql.Extensions.DynamicEntity
|
|||||||
|
|
||||||
if (tableAttribute == null) continue;
|
if (tableAttribute == null) continue;
|
||||||
|
|
||||||
var classCtorInfo = tableAttribute.GetType().GetConstructor(new Type[] { });
|
var classCtorInfo = tableAttribute.GetType().GetConstructor(Type.EmptyTypes);
|
||||||
|
|
||||||
var propertyInfos = tableAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
|
var propertyInfos = tableAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
|
||||||
|
|
||||||
@ -152,6 +212,7 @@ namespace FreeSql.Extensions.DynamicEntity
|
|||||||
|
|
||||||
private void SetPropertys(ref TypeBuilder typeBuilder)
|
private void SetPropertys(ref TypeBuilder typeBuilder)
|
||||||
{
|
{
|
||||||
|
var defaultValues = new Dictionary<FieldBuilder, object>();
|
||||||
foreach (var pinfo in _properties)
|
foreach (var pinfo in _properties)
|
||||||
{
|
{
|
||||||
if (pinfo == null)
|
if (pinfo == null)
|
||||||
@ -160,12 +221,20 @@ namespace FreeSql.Extensions.DynamicEntity
|
|||||||
var propertyType = pinfo.PropertyType;
|
var propertyType = pinfo.PropertyType;
|
||||||
//设置字段
|
//设置字段
|
||||||
var field = typeBuilder.DefineField($"_{FirstCharToLower(propertyName)}", propertyType,
|
var field = typeBuilder.DefineField($"_{FirstCharToLower(propertyName)}", propertyType,
|
||||||
FieldAttributes.Private);
|
FieldAttributes.Private | FieldAttributes.HasDefault);
|
||||||
var firstCharToUpper = FirstCharToUpper(propertyName);
|
var firstCharToUpper = FirstCharToUpper(propertyName);
|
||||||
|
|
||||||
|
MethodAttributes maAttributes = MethodAttributes.Public;
|
||||||
|
|
||||||
|
//是否重写
|
||||||
|
if (pinfo.IsOverride)
|
||||||
|
{
|
||||||
|
maAttributes = MethodAttributes.Public | MethodAttributes.Virtual;
|
||||||
|
}
|
||||||
|
|
||||||
//设置属性方法
|
//设置属性方法
|
||||||
var methodGet = typeBuilder.DefineMethod($"Get{firstCharToUpper}", MethodAttributes.Public,
|
var methodGet = typeBuilder.DefineMethod($"get_{firstCharToUpper}", maAttributes, propertyType, null);
|
||||||
propertyType, null);
|
var methodSet = typeBuilder.DefineMethod($"set_{firstCharToUpper}", maAttributes, null,
|
||||||
var methodSet = typeBuilder.DefineMethod($"Set{firstCharToUpper}", MethodAttributes.Public, null,
|
|
||||||
new Type[] { propertyType });
|
new Type[] { propertyType });
|
||||||
|
|
||||||
var ilOfGet = methodGet.GetILGenerator();
|
var ilOfGet = methodGet.GetILGenerator();
|
||||||
@ -179,6 +248,15 @@ namespace FreeSql.Extensions.DynamicEntity
|
|||||||
ilOfSet.Emit(OpCodes.Stfld, field);
|
ilOfSet.Emit(OpCodes.Stfld, field);
|
||||||
ilOfSet.Emit(OpCodes.Ret);
|
ilOfSet.Emit(OpCodes.Ret);
|
||||||
|
|
||||||
|
|
||||||
|
//是否重写
|
||||||
|
if (pinfo.IsOverride)
|
||||||
|
{
|
||||||
|
//重写Get、Set方法
|
||||||
|
OverrideProperty(ref typeBuilder, methodGet, PropertyMethodEnum.GET, pinfo.PropertyName);
|
||||||
|
OverrideProperty(ref typeBuilder, methodSet, PropertyMethodEnum.SET, pinfo.PropertyName);
|
||||||
|
}
|
||||||
|
|
||||||
//设置属性
|
//设置属性
|
||||||
var propertyBuilder =
|
var propertyBuilder =
|
||||||
typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);
|
typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);
|
||||||
@ -190,24 +268,109 @@ namespace FreeSql.Extensions.DynamicEntity
|
|||||||
//设置特性
|
//设置特性
|
||||||
SetPropertyAttribute(ref propertyBuilder, pinfoAttribute);
|
SetPropertyAttribute(ref propertyBuilder, pinfoAttribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pinfo.DefaultValue != null)
|
||||||
|
{
|
||||||
|
defaultValues.Add(field, pinfo.DefaultValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//动态构建方法,设置默认值
|
||||||
|
var methodDefaultValue = typeBuilder.DefineMethod($"DefaultValueInit", MethodAttributes.Public, null, null);
|
||||||
|
var methodDefaultValueLlGenerator = methodDefaultValue.GetILGenerator();
|
||||||
|
foreach (var kv in defaultValues)
|
||||||
|
{
|
||||||
|
methodDefaultValueLlGenerator.Emit(OpCodes.Ldarg_0);
|
||||||
|
OpCodesAdapter(ref methodDefaultValueLlGenerator, kv.Key, kv.Value);
|
||||||
|
methodDefaultValueLlGenerator.Emit(OpCodes.Stfld, kv.Key);
|
||||||
|
}
|
||||||
|
|
||||||
|
methodDefaultValueLlGenerator.Emit(OpCodes.Ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IL命令类型适配
|
||||||
|
private void OpCodesAdapter(ref ILGenerator generator, FieldInfo info, object value)
|
||||||
|
{
|
||||||
|
var fieldTypeName = info.FieldType.Name;
|
||||||
|
switch (fieldTypeName)
|
||||||
|
{
|
||||||
|
case "Int32":
|
||||||
|
generator.Emit(OpCodes.Ldc_I4, Convert.ToInt32(value));
|
||||||
|
break;
|
||||||
|
case "Boolean":
|
||||||
|
generator.Emit(OpCodes.Ldc_I4, Convert.ToInt32(value));
|
||||||
|
break;
|
||||||
|
case "Char":
|
||||||
|
generator.Emit(OpCodes.Ldc_I4, Convert.ToChar(value));
|
||||||
|
break;
|
||||||
|
case "String":
|
||||||
|
generator.Emit(OpCodes.Ldstr, Convert.ToString(value));
|
||||||
|
break;
|
||||||
|
case "DateTime":
|
||||||
|
generator.Emit(OpCodes.Ldstr, Convert.ToString(value));
|
||||||
|
generator.Emit(OpCodes.Call, typeof(DateTime).GetMethod("Parse", new[] { typeof(string) }));
|
||||||
|
break;
|
||||||
|
case "Int64":
|
||||||
|
generator.Emit(OpCodes.Ldc_I4, Convert.ToString(value));
|
||||||
|
generator.Emit(OpCodes.Conv_I8);
|
||||||
|
break;
|
||||||
|
case "Double":
|
||||||
|
generator.Emit(OpCodes.Ldc_R8, Convert.ToDouble(value));
|
||||||
|
break;
|
||||||
|
case "Single":
|
||||||
|
generator.Emit(OpCodes.Ldc_R4, Convert.ToSingle(value));
|
||||||
|
break;
|
||||||
|
case "Decimal":
|
||||||
|
Console.WriteLine(Convert.ToString(value));
|
||||||
|
generator.Emit(OpCodes.Ldstr, Convert.ToString(value));
|
||||||
|
generator.Emit(OpCodes.Call, typeof(Decimal).GetMethod("Parse", new[] { typeof(string) }));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetPropertyAttribute<T>(ref PropertyBuilder propertyBuilder, T tAttribute)
|
private void SetPropertyAttribute<T>(ref PropertyBuilder propertyBuilder, T tAttribute)
|
||||||
{
|
{
|
||||||
if (tAttribute == null) return;
|
if (tAttribute == null) return;
|
||||||
|
|
||||||
var propertyInfos = tAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
|
var propertyInfos = tAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
|
||||||
var constructor = tAttribute.GetType().GetConstructor(new Type[] { });
|
var constructor = tAttribute.GetType().GetConstructor(Type.EmptyTypes);
|
||||||
var propertyValues = new ArrayList();
|
var propertyValues = new ArrayList();
|
||||||
foreach (var propertyInfo in propertyInfos)
|
foreach (var propertyInfo in propertyInfos)
|
||||||
propertyValues.Add(propertyInfo.GetValue(tAttribute));
|
propertyValues.Add(propertyInfo.GetValue(tAttribute));
|
||||||
|
|
||||||
var customAttributeBuilder =
|
//可能存在有参构造
|
||||||
new CustomAttributeBuilder(constructor, new object[0], propertyInfos, propertyValues.ToArray());
|
//if (constructor == null)
|
||||||
|
//{
|
||||||
|
// var constructorTypes = propertyInfos.Select(p => p.PropertyType).ToList();
|
||||||
|
// constructor = tAttribute.GetType().GetConstructor(constructorTypes.ToArray());
|
||||||
|
// var customAttributeBuilder = new CustomAttributeBuilder(constructor, constructorTypes.ToArray(),
|
||||||
|
// propertyInfos, propertyValues.ToArray());
|
||||||
|
// propertyBuilder.SetCustomAttribute(customAttributeBuilder);
|
||||||
|
//}
|
||||||
|
//else
|
||||||
|
//{
|
||||||
|
var customAttributeBuilder = new CustomAttributeBuilder(constructor, new object[0], propertyInfos,
|
||||||
|
propertyValues.ToArray());
|
||||||
propertyBuilder.SetCustomAttribute(customAttributeBuilder);
|
propertyBuilder.SetCustomAttribute(customAttributeBuilder);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Override属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="typeBuilder"></param>
|
||||||
|
private void OverrideProperty(ref TypeBuilder typeBuilder, MethodBuilder methodBuilder,
|
||||||
|
PropertyMethodEnum methodEnum,
|
||||||
|
string propertyName)
|
||||||
|
{
|
||||||
|
//查找父类的属性信息
|
||||||
|
var propertyInfo = typeBuilder.BaseType.GetProperty(propertyName);
|
||||||
|
if (propertyInfo == null) return;
|
||||||
|
var pm = methodEnum == PropertyMethodEnum.GET ? propertyInfo.GetGetMethod() : propertyInfo.GetSetMethod();
|
||||||
|
//重写父类GET SET 方法
|
||||||
|
typeBuilder.DefineMethodOverride(methodBuilder, pm);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Emit动态创建出Class - Type
|
/// Emit动态创建出Class - Type
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -233,6 +396,7 @@ namespace FreeSql.Extensions.DynamicEntity
|
|||||||
|
|
||||||
//创建类的Type对象
|
//创建类的Type对象
|
||||||
var type = typeBuilder.CreateType();
|
var type = typeBuilder.CreateType();
|
||||||
|
|
||||||
return _fsql.CodeFirst.GetTableByEntity(type);
|
return _fsql.CodeFirst.GetTableByEntity(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -279,8 +443,16 @@ namespace FreeSql.Extensions.DynamicEntity
|
|||||||
{
|
{
|
||||||
public string PropertyName { get; set; } = string.Empty;
|
public string PropertyName { get; set; } = string.Empty;
|
||||||
public Type PropertyType { get; set; }
|
public Type PropertyType { get; set; }
|
||||||
|
public object DefaultValue { get; set; }
|
||||||
|
public bool IsOverride { get; set; } = false;
|
||||||
public Attribute[] Attributes { get; set; }
|
public Attribute[] Attributes { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum PropertyMethodEnum
|
||||||
|
{
|
||||||
|
GET,
|
||||||
|
SET
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
@ -1073,6 +1073,82 @@
|
|||||||
</summary>
|
</summary>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="T:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder">
|
||||||
|
<summary>
|
||||||
|
动态创建实体类型
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder.#ctor(IFreeSql,System.String,System.Attribute[])">
|
||||||
|
<summary>
|
||||||
|
配置Class
|
||||||
|
</summary>
|
||||||
|
<param name="className">类名</param>
|
||||||
|
<param name="attributes">类标记的特性[Table(Name = "xxx")] [Index(xxxx)]</param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder.Property(System.String,System.Type,System.Attribute[])">
|
||||||
|
<summary>
|
||||||
|
配置属性
|
||||||
|
</summary>
|
||||||
|
<param name="propertyName">属性名称</param>
|
||||||
|
<param name="propertyType">属性类型</param>
|
||||||
|
<param name="attributes">属性标记的特性-支持多个</param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder.Property(System.String,System.Type,System.Boolean,System.Attribute[])">
|
||||||
|
<summary>
|
||||||
|
配置属性
|
||||||
|
</summary>
|
||||||
|
<param name="propertyName">属性名称</param>
|
||||||
|
<param name="propertyType">属性类型</param>
|
||||||
|
<param name="isOverride">该属性是否重写父类属性</param>
|
||||||
|
<param name="attributes">属性标记的特性-支持多个</param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder.Property(System.String,System.Type,System.Boolean,System.Object,System.Attribute[])">
|
||||||
|
<summary>
|
||||||
|
配置属性
|
||||||
|
</summary>
|
||||||
|
<param name="propertyName">属性名称</param>
|
||||||
|
<param name="propertyType">属性类型</param>
|
||||||
|
<param name="isOverride">该属性是否重写父类属性</param>
|
||||||
|
<param name="defaultValue">属性默认值</param>
|
||||||
|
<param name="attributes">属性标记的特性-支持多个</param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder.Extend(System.Type)">
|
||||||
|
<summary>
|
||||||
|
配置父类
|
||||||
|
</summary>
|
||||||
|
<param name="superClass">父类类型</param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder.OverrideProperty(System.Reflection.Emit.TypeBuilder@,System.Reflection.Emit.MethodBuilder,FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder.PropertyMethodEnum,System.String)">
|
||||||
|
<summary>
|
||||||
|
Override属性
|
||||||
|
</summary>
|
||||||
|
<param name="typeBuilder"></param>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder.Build">
|
||||||
|
<summary>
|
||||||
|
Emit动态创建出Class - Type
|
||||||
|
</summary>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder.FirstCharToLower(System.String)">
|
||||||
|
<summary>
|
||||||
|
首字母小写
|
||||||
|
</summary>
|
||||||
|
<param name="input"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder.FirstCharToUpper(System.String)">
|
||||||
|
<summary>
|
||||||
|
首字母大写
|
||||||
|
</summary>
|
||||||
|
<param name="input"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
<member name="M:FreeSql.Extensions.EntityUtil.EntityUtilExtensions.GetEntityKeyString(IFreeSql,System.Type,System.Object,System.Boolean,System.String)">
|
<member name="M:FreeSql.Extensions.EntityUtil.EntityUtilExtensions.GetEntityKeyString(IFreeSql,System.Type,System.Object,System.Boolean,System.String)">
|
||||||
<summary>
|
<summary>
|
||||||
获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值时,返回 null
|
获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值时,返回 null
|
||||||
@ -5692,6 +5768,28 @@
|
|||||||
请使用 fsql.InsertDict(dict) 方法插入字典数据
|
请使用 fsql.InsertDict(dict) 方法插入字典数据
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:FreeSqlGlobalDynamicEntityExtensions.DynamicEntity(FreeSql.ICodeFirst,System.String,System.Attribute[])">
|
||||||
|
<summary>
|
||||||
|
动态构建Class Type
|
||||||
|
</summary>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSqlGlobalDynamicEntityExtensions.CreateInstance(FreeSql.Internal.Model.TableInfo,System.Collections.Generic.Dictionary{System.String,System.Object})">
|
||||||
|
<summary>
|
||||||
|
根据字典,创建 table 对应的实体对象
|
||||||
|
</summary>
|
||||||
|
<param name="table"></param>
|
||||||
|
<param name="dict"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSqlGlobalDynamicEntityExtensions.CreateDictionary(FreeSql.Internal.Model.TableInfo,System.Object)">
|
||||||
|
<summary>
|
||||||
|
根据实体对象,创建 table 对应的字典
|
||||||
|
</summary>
|
||||||
|
<param name="table"></param>
|
||||||
|
<param name="instance"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
<member name="M:FreeSqlGlobalExpressionCallExtensions.Between(System.DateTime,System.DateTime,System.DateTime)">
|
<member name="M:FreeSqlGlobalExpressionCallExtensions.Between(System.DateTime,System.DateTime,System.DateTime)">
|
||||||
<summary>
|
<summary>
|
||||||
C#: that >= between && that <= and<para></para>
|
C#: that >= between && that <= and<para></para>
|
||||||
@ -6208,81 +6306,3 @@
|
|||||||
</member>
|
</member>
|
||||||
</members>
|
</members>
|
||||||
</doc>
|
</doc>
|
||||||
ew{id=1}
|
|
||||||
</summary>
|
|
||||||
<typeparam name="T1"></typeparam>
|
|
||||||
<param name="dywhere">主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合</param>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:IFreeSql.Select``1">
|
|
||||||
<summary>
|
|
||||||
查询数据
|
|
||||||
</summary>
|
|
||||||
<typeparam name="T1"></typeparam>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:IFreeSql.Select``1(System.Object)">
|
|
||||||
<summary>
|
|
||||||
查询数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1}
|
|
||||||
</summary>
|
|
||||||
<typeparam name="T1"></typeparam>
|
|
||||||
<param name="dywhere">主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合</param>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:IFreeSql.Delete``1">
|
|
||||||
<summary>
|
|
||||||
删除数据
|
|
||||||
</summary>
|
|
||||||
<typeparam name="T1"></typeparam>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:IFreeSql.Delete``1(System.Object)">
|
|
||||||
<summary>
|
|
||||||
删除数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1}
|
|
||||||
</summary>
|
|
||||||
<typeparam name="T1"></typeparam>
|
|
||||||
<param name="dywhere">主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合</param>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:IFreeSql.Transaction(System.Action)">
|
|
||||||
<summary>
|
|
||||||
开启事务(不支持异步)<para></para>
|
|
||||||
v1.5.0 关闭了线程事务超时自动提交的机制
|
|
||||||
</summary>
|
|
||||||
<param name="handler">事务体 () => {}</param>
|
|
||||||
</member>
|
|
||||||
<member name="M:IFreeSql.Transaction(System.Data.IsolationLevel,System.Action)">
|
|
||||||
<summary>
|
|
||||||
开启事务(不支持异步)<para></para>
|
|
||||||
v1.5.0 关闭了线程事务超时自动提交的机制
|
|
||||||
</summary>
|
|
||||||
<param name="isolationLevel"></param>
|
|
||||||
<param name="handler">事务体 () => {}</param>
|
|
||||||
</member>
|
|
||||||
<member name="P:IFreeSql.Ado">
|
|
||||||
<summary>
|
|
||||||
数据库访问对象
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
<member name="P:IFreeSql.Aop">
|
|
||||||
<summary>
|
|
||||||
所有拦截方法都在这里
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
<member name="P:IFreeSql.CodeFirst">
|
|
||||||
<summary>
|
|
||||||
CodeFirst 模式开发相关方法
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
<member name="P:IFreeSql.DbFirst">
|
|
||||||
<summary>
|
|
||||||
DbFirst 模式开发相关方法
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
<member name="P:IFreeSql.GlobalFilter">
|
|
||||||
<summary>
|
|
||||||
全局过滤设置,可默认附加为 Select/Update/Delete 条件
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
</members>
|
|
||||||
</doc>
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user