mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
commit
2945f954ae
@ -1,36 +1,35 @@
|
|||||||
using FreeSql.DataAnnotations;
|
using System;
|
||||||
using System;
|
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Reflection.Emit;
|
using System.Reflection.Emit;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using FreeSql.Internal.Model;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FreeSql.Internal
|
namespace FreeSql.Extensions.DynamicEntity
|
||||||
{
|
{
|
||||||
#if net40 || NETSTANDARD2_0
|
/// <summary>
|
||||||
#else
|
/// 动态创建实体类型
|
||||||
|
/// </summary>
|
||||||
public class DynamicCompileBuilder
|
public class DynamicCompileBuilder
|
||||||
{
|
{
|
||||||
private string _className = string.Empty;
|
private string _className = string.Empty;
|
||||||
private Attribute[] _tableAttributes = null;
|
private Attribute[] _tableAttributes = null;
|
||||||
private List<DynamicPropertyInfo> _properties = new List<DynamicPropertyInfo>();
|
private List<DynamicPropertyInfo> _properties = new List<DynamicPropertyInfo>();
|
||||||
|
private Type _superClass = null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 配置Class
|
/// 配置Class
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="className">类名</param>
|
/// <param name="className">类名</param>
|
||||||
/// <param name="attributes">类标记的特性[Table(Name = "xxx")]</param>
|
/// <param name="attributes">类标记的特性[Table(Name = "xxx")] [Index(xxxx)]</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public DynamicCompileBuilder(string className, params Attribute[] attributes)
|
public DynamicCompileBuilder Class(string className, params Attribute[] attributes)
|
||||||
{
|
{
|
||||||
_className = className;
|
_className = className;
|
||||||
_tableAttributes = attributes;
|
_tableAttributes = attributes;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -51,22 +50,48 @@ namespace FreeSql.Internal
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 配置父类
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="superClass">父类类型</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public DynamicCompileBuilder Extend(Type superClass)
|
||||||
|
{
|
||||||
|
_superClass = superClass;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
private void SetTableAttribute(ref TypeBuilder typeBuilder)
|
private void SetTableAttribute(ref TypeBuilder typeBuilder)
|
||||||
{
|
{
|
||||||
if (_tableAttributes == null) return;
|
if (_tableAttributes == null) return;
|
||||||
|
|
||||||
var propertyValues = new ArrayList();
|
|
||||||
foreach (var tableAttribute in _tableAttributes)
|
foreach (var tableAttribute in _tableAttributes)
|
||||||
{
|
{
|
||||||
|
var propertyValues = new ArrayList();
|
||||||
|
|
||||||
if (tableAttribute == null) continue;
|
if (tableAttribute == null) continue;
|
||||||
|
|
||||||
var classCtorInfo = tableAttribute.GetType().GetConstructor(new Type[] { });
|
var classCtorInfo = tableAttribute.GetType().GetConstructor(new Type[] { });
|
||||||
|
|
||||||
var propertyInfos = tableAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
|
var propertyInfos = tableAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
|
||||||
|
|
||||||
foreach (var propertyInfo in propertyInfos)
|
foreach (var propertyInfo in propertyInfos)
|
||||||
propertyValues.Add(propertyInfo.GetValue(tableAttribute));
|
propertyValues.Add(propertyInfo.GetValue(tableAttribute));
|
||||||
|
|
||||||
var customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo, new object[0], propertyInfos, propertyValues.ToArray());
|
//可能存在有参构造
|
||||||
typeBuilder.SetCustomAttribute(customAttributeBuilder);
|
if (classCtorInfo == null)
|
||||||
|
{
|
||||||
|
var constructorTypes = propertyInfos.Select(p => p.PropertyType);
|
||||||
|
classCtorInfo = tableAttribute.GetType().GetConstructor(constructorTypes.ToArray());
|
||||||
|
var customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo, propertyValues.ToArray());
|
||||||
|
typeBuilder.SetCustomAttribute(customAttributeBuilder);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo, new object[0], propertyInfos,
|
||||||
|
propertyValues.ToArray());
|
||||||
|
typeBuilder.SetCustomAttribute(customAttributeBuilder);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,8 +99,10 @@ namespace FreeSql.Internal
|
|||||||
{
|
{
|
||||||
foreach (var pinfo in _properties)
|
foreach (var pinfo in _properties)
|
||||||
{
|
{
|
||||||
|
if (pinfo == null)
|
||||||
|
continue;
|
||||||
var propertyName = pinfo.PropertyName;
|
var propertyName = pinfo.PropertyName;
|
||||||
var propertyType = pinfo?.PropertyType ?? typeof(object);
|
var propertyType = pinfo.PropertyType;
|
||||||
//设置字段
|
//设置字段
|
||||||
var field = typeBuilder.DefineField($"_{FirstCharToLower(propertyName)}", propertyType,
|
var field = typeBuilder.DefineField($"_{FirstCharToLower(propertyName)}", propertyType,
|
||||||
FieldAttributes.Private);
|
FieldAttributes.Private);
|
||||||
@ -98,7 +125,8 @@ namespace FreeSql.Internal
|
|||||||
ilOfSet.Emit(OpCodes.Ret);
|
ilOfSet.Emit(OpCodes.Ret);
|
||||||
|
|
||||||
//设置属性
|
//设置属性
|
||||||
var propertyBuilder = typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);
|
var propertyBuilder =
|
||||||
|
typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);
|
||||||
propertyBuilder.SetGetMethod(methodGet);
|
propertyBuilder.SetGetMethod(methodGet);
|
||||||
propertyBuilder.SetSetMethod(methodSet);
|
propertyBuilder.SetSetMethod(methodSet);
|
||||||
|
|
||||||
@ -120,7 +148,8 @@ namespace FreeSql.Internal
|
|||||||
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());
|
var customAttributeBuilder =
|
||||||
|
new CustomAttributeBuilder(constructor, new object[0], propertyInfos, propertyValues.ToArray());
|
||||||
propertyBuilder.SetCustomAttribute(customAttributeBuilder);
|
propertyBuilder.SetCustomAttribute(customAttributeBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +167,8 @@ namespace FreeSql.Internal
|
|||||||
var defineDynamicModule =
|
var defineDynamicModule =
|
||||||
defineDynamicAssembly.DefineDynamicModule("FreeSql.DynamicCompileBuilder.Dynamics");
|
defineDynamicAssembly.DefineDynamicModule("FreeSql.DynamicCompileBuilder.Dynamics");
|
||||||
//动态的在模块内创建一个类
|
//动态的在模块内创建一个类
|
||||||
var typeBuilder = defineDynamicModule.DefineType(_className, TypeAttributes.Public | TypeAttributes.Class);
|
var typeBuilder =
|
||||||
|
defineDynamicModule.DefineType(_className, TypeAttributes.Public | TypeAttributes.Class, _superClass);
|
||||||
|
|
||||||
//设置TableAttribute
|
//设置TableAttribute
|
||||||
SetTableAttribute(ref typeBuilder);
|
SetTableAttribute(ref typeBuilder);
|
||||||
@ -150,70 +180,6 @@ namespace FreeSql.Internal
|
|||||||
return typeBuilder.CreateType();
|
return typeBuilder.CreateType();
|
||||||
}
|
}
|
||||||
|
|
||||||
//委托缓存
|
|
||||||
private static ConcurrentDictionary<string, Delegate>
|
|
||||||
_delegateCache = new ConcurrentDictionary<string, Delegate>();
|
|
||||||
|
|
||||||
//设置动态对象的属性值 使用FreeSql自带功能
|
|
||||||
public static object CreateObjectByTypeByCodeFirst(IFreeSql fsql, Type type,
|
|
||||||
Dictionary<string, object> porpertys)
|
|
||||||
{
|
|
||||||
if (type == null)
|
|
||||||
return null;
|
|
||||||
object istance = Activator.CreateInstance(type);
|
|
||||||
if (istance == null)
|
|
||||||
return null;
|
|
||||||
var table = fsql.CodeFirst.GetTableByEntity(type);
|
|
||||||
foreach (var kv in porpertys)
|
|
||||||
{
|
|
||||||
table.ColumnsByCs[kv.Key].SetValue(istance, kv.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return istance;
|
|
||||||
}
|
|
||||||
|
|
||||||
////设置动态对象的属性值,使用表达式目录树
|
|
||||||
//public static object CreateObjectByType(Type type, Dictionary<string, object> porpertys)
|
|
||||||
//{
|
|
||||||
// if (type == null)
|
|
||||||
// return null;
|
|
||||||
// object istance = Activator.CreateInstance(type);
|
|
||||||
// if (istance == null)
|
|
||||||
// return null;
|
|
||||||
// //根据字典中的key确定缓存
|
|
||||||
// var cacheKeyStr = string.Join("-", porpertys.Keys.OrderBy(s => s));
|
|
||||||
// var cacheKey = Md5Encryption(cacheKeyStr);
|
|
||||||
// var dynamicDelegate = _delegateCache.GetOrAdd(cacheKey, key =>
|
|
||||||
// {
|
|
||||||
// //表达式目录树构建委托
|
|
||||||
// var typeParam = Expression.Parameter(type);
|
|
||||||
// var dicParamType = typeof(Dictionary<string, object>);
|
|
||||||
// var dicParam = Expression.Parameter(dicParamType);
|
|
||||||
// var exps = new List<Expression>();
|
|
||||||
// var tempRef = Expression.Variable(typeof(object));
|
|
||||||
// foreach (var pinfo in porpertys)
|
|
||||||
// {
|
|
||||||
// var propertyInfo = type.GetProperty(pinfo.Key);
|
|
||||||
// if (propertyInfo == null)
|
|
||||||
// continue;
|
|
||||||
// var propertyName = Expression.Constant(pinfo.Key, typeof(string));
|
|
||||||
// exps.Add(Expression.Call(dicParam, dicParamType.GetMethod("TryGetValue"), propertyName, tempRef));
|
|
||||||
// exps.Add(Expression.Assign(Expression.MakeMemberAccess(typeParam, propertyInfo),
|
|
||||||
// Expression.Convert(tempRef, propertyInfo.PropertyType)));
|
|
||||||
// exps.Add(Expression.Assign(tempRef, Expression.Default(typeof(object))));
|
|
||||||
// }
|
|
||||||
|
|
||||||
// var returnTarget = Expression.Label(type);
|
|
||||||
// exps.Add(Expression.Return(returnTarget, typeParam));
|
|
||||||
// exps.Add(Expression.Label(returnTarget, Expression.Default(type)));
|
|
||||||
// var block = Expression.Block(new[] { tempRef }, exps);
|
|
||||||
// var @delegate = Expression.Lambda(block, typeParam, dicParam).Compile();
|
|
||||||
// return @delegate;
|
|
||||||
// });
|
|
||||||
// var dynamicInvoke = dynamicDelegate.DynamicInvoke(istance, porpertys);
|
|
||||||
// return dynamicInvoke;
|
|
||||||
//}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 首字母小写
|
/// 首字母小写
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -260,5 +226,4 @@ namespace FreeSql.Internal
|
|||||||
public Attribute[] Attributes { get; set; }
|
public Attribute[] Attributes { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
@ -0,0 +1,92 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql.Extensions.DynamicEntity
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 动态创建对象帮助类
|
||||||
|
/// </summary>
|
||||||
|
public class DynamicCompileHelper
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 动态构建Class - Type
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static DynamicCompileBuilder DynamicBuilder()
|
||||||
|
{
|
||||||
|
return new DynamicCompileBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 委托缓存
|
||||||
|
/// </summary>
|
||||||
|
private static readonly ConcurrentDictionary<string, Delegate> DelegateCache =
|
||||||
|
new ConcurrentDictionary<string, Delegate>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置动态对象的属性值
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type"></param>
|
||||||
|
/// <param name="porpertys"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static object CreateObjectByType(Type type, Dictionary<string, object> porpertys)
|
||||||
|
{
|
||||||
|
if (type == null)
|
||||||
|
return null;
|
||||||
|
object istance = Activator.CreateInstance(type);
|
||||||
|
if (istance == null)
|
||||||
|
return null;
|
||||||
|
//根据key确定缓存
|
||||||
|
var cacheKeyStr = string.Join("-", porpertys.Keys.OrderBy(s => s));
|
||||||
|
var dicKey = Md5Encryption(cacheKeyStr);
|
||||||
|
var cacheKey = $"{type.GetHashCode()}-{dicKey}";
|
||||||
|
var dynamicDelegate = DelegateCache.GetOrAdd(cacheKey, key =>
|
||||||
|
{
|
||||||
|
//表达式目录树构建委托
|
||||||
|
var typeParam = Expression.Parameter(type);
|
||||||
|
var dicParamType = typeof(Dictionary<string, object>);
|
||||||
|
var dicParam = Expression.Parameter(dicParamType);
|
||||||
|
var exps = new List<Expression>();
|
||||||
|
var tempRef = Expression.Variable(typeof(object));
|
||||||
|
foreach (var pinfo in porpertys)
|
||||||
|
{
|
||||||
|
var propertyInfo = type.GetProperty(pinfo.Key);
|
||||||
|
if (propertyInfo == null)
|
||||||
|
continue;
|
||||||
|
var propertyName = Expression.Constant(pinfo.Key, typeof(string));
|
||||||
|
exps.Add(Expression.Call(dicParam, dicParamType.GetMethod("TryGetValue"), propertyName, tempRef));
|
||||||
|
exps.Add(Expression.Assign(Expression.MakeMemberAccess(typeParam, propertyInfo),
|
||||||
|
Expression.Convert(tempRef, propertyInfo.PropertyType)));
|
||||||
|
exps.Add(Expression.Assign(tempRef, Expression.Default(typeof(object))));
|
||||||
|
}
|
||||||
|
|
||||||
|
var returnTarget = Expression.Label(type);
|
||||||
|
exps.Add(Expression.Return(returnTarget, typeParam));
|
||||||
|
exps.Add(Expression.Label(returnTarget, Expression.Default(type)));
|
||||||
|
var block = Expression.Block(new[] { tempRef }, exps);
|
||||||
|
var @delegate = Expression.Lambda(block, typeParam, dicParam).Compile();
|
||||||
|
return @delegate;
|
||||||
|
});
|
||||||
|
var dynamicInvoke = dynamicDelegate.DynamicInvoke(istance, porpertys);
|
||||||
|
return dynamicInvoke;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string Md5Encryption(string inputStr)
|
||||||
|
{
|
||||||
|
var result = string.Empty;
|
||||||
|
//32位大写
|
||||||
|
using (var md5 = MD5.Create())
|
||||||
|
{
|
||||||
|
var resultBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(inputStr));
|
||||||
|
result = BitConverter.ToString(resultBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFrameworks>netstandard2.1;net451;net45;</TargetFrameworks>
|
||||||
|
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||||
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
|
<Authors>FreeSql;ncc;YeXiangQin;Daily</Authors>
|
||||||
|
<Description>FreeSql 扩展包,可实现动态构建实体类,动态创建表.</Description>
|
||||||
|
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
||||||
|
<RepositoryUrl>https://github.com/2881099/FreeSql</RepositoryUrl>
|
||||||
|
<RepositoryType>git</RepositoryType>
|
||||||
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
|
<PackageTags>FreeSql;ORM</PackageTags>
|
||||||
|
<PackageId>$(AssemblyName)</PackageId>
|
||||||
|
<PackageIcon>logo.png</PackageIcon>
|
||||||
|
<Title>$(AssemblyName)</Title>
|
||||||
|
<IsPackable>true</IsPackable>
|
||||||
|
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||||
|
<SignAssembly>true</SignAssembly>
|
||||||
|
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
|
||||||
|
<DelaySign>false</DelaySign>
|
||||||
|
<Version>3.2.694</Version> <PackageId>$(AssemblyName)</PackageId>
|
||||||
|
<PackageIcon>logo.png</PackageIcon>
|
||||||
|
<Title>$(AssemblyName)</Title>
|
||||||
|
<IsPackable>true</IsPackable>
|
||||||
|
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||||
|
<SignAssembly>true</SignAssembly>
|
||||||
|
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
|
||||||
|
<DelaySign>false</DelaySign>
|
||||||
|
<DocumentationFile>FreeSql.Extensions.DynamicEntity.xml</DocumentationFile>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="../../logo.png" Pack="true" PackagePath="\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="FreeSql.Extensions.DynamicEntity.xml">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -0,0 +1,81 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<doc>
|
||||||
|
<assembly>
|
||||||
|
<name>FreeSql.Extensions.DynamicEntity</name>
|
||||||
|
</assembly>
|
||||||
|
<members>
|
||||||
|
<member name="T:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder">
|
||||||
|
<summary>
|
||||||
|
动态创建实体类型
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileBuilder.Class(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.Extend(System.Type)">
|
||||||
|
<summary>
|
||||||
|
配置父类
|
||||||
|
</summary>
|
||||||
|
<param name="superClass">父类类型</param>
|
||||||
|
<returns></returns>
|
||||||
|
</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="T:FreeSql.Extensions.DynamicEntity.DynamicCompileHelper">
|
||||||
|
<summary>
|
||||||
|
动态创建对象帮助类
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileHelper.DynamicBuilder">
|
||||||
|
<summary>
|
||||||
|
动态构建Class - Type
|
||||||
|
</summary>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="F:FreeSql.Extensions.DynamicEntity.DynamicCompileHelper.DelegateCache">
|
||||||
|
<summary>
|
||||||
|
委托缓存
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Extensions.DynamicEntity.DynamicCompileHelper.CreateObjectByType(System.Type,System.Collections.Generic.Dictionary{System.String,System.Object})">
|
||||||
|
<summary>
|
||||||
|
设置动态对象的属性值
|
||||||
|
</summary>
|
||||||
|
<param name="type"></param>
|
||||||
|
<param name="porpertys"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
</members>
|
||||||
|
</doc>
|
BIN
Extensions/FreeSql.Extensions.DynamicEntity/key.snk
Normal file
BIN
Extensions/FreeSql.Extensions.DynamicEntity/key.snk
Normal file
Binary file not shown.
102
FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs
Normal file
102
FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using FreeSql.Extensions.DynamicEntity;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.DynamicEntity
|
||||||
|
{
|
||||||
|
public class DynamicEntityTest
|
||||||
|
{
|
||||||
|
private static IFreeSql fsql = new FreeSqlBuilder().UseConnectionString(DataType.PostgreSQL,
|
||||||
|
"Host=192.168.0.36;Port=5432;Username=postgres;Password=123; Database=test;ArrayNullabilityMode=Always;Pooling=true;Minimum Pool Size=1")
|
||||||
|
.UseMonitorCommand(d => Console.WriteLine(d.CommandText)).Build();
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NormalTest()
|
||||||
|
{
|
||||||
|
Type type = DynamicCompileHelper.DynamicBuilder()
|
||||||
|
.Class("NormalUsers")
|
||||||
|
.Property("Id", typeof(string))
|
||||||
|
.Property("Name", typeof(string))
|
||||||
|
.Property("Address", typeof(string))
|
||||||
|
.Build();
|
||||||
|
var dict = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["Name"] = "张三",
|
||||||
|
["Id"] = Guid.NewGuid().ToString(),
|
||||||
|
["Address"] = "北京市"
|
||||||
|
};
|
||||||
|
var instance = DynamicCompileHelper.CreateObjectByType(type, dict);
|
||||||
|
//根据Type生成表
|
||||||
|
fsql.CodeFirst.SyncStructure(type);
|
||||||
|
fsql.Insert<object>().AsType(type).AppendData(instance).ExecuteAffrows();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AttributeTest()
|
||||||
|
{
|
||||||
|
Type type = DynamicCompileHelper.DynamicBuilder()
|
||||||
|
.Class("AttributeUsers", new TableAttribute() { Name = "T_Attribute_User" },
|
||||||
|
new IndexAttribute("Name_Index", "Name", false))
|
||||||
|
.Property("Id", typeof(int),
|
||||||
|
new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 })
|
||||||
|
.Property("Name", typeof(string),
|
||||||
|
new ColumnAttribute() { StringLength = 20, Position = 2 })
|
||||||
|
.Property("Address", typeof(string),
|
||||||
|
new ColumnAttribute() { StringLength = 150, Position = 3 })
|
||||||
|
.Build();
|
||||||
|
var dict = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["Name"] = "张三",
|
||||||
|
["Address"] = "北京市"
|
||||||
|
};
|
||||||
|
var instance = DynamicCompileHelper.CreateObjectByType(type, dict);
|
||||||
|
//根据Type生成表
|
||||||
|
fsql.CodeFirst.SyncStructure(type);
|
||||||
|
var insertId = fsql.Insert<object>().AsType(type).AppendData(instance).ExecuteIdentity();
|
||||||
|
var select = fsql.Select<object>().AsType(type).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SuperClassTest()
|
||||||
|
{
|
||||||
|
Type type = DynamicCompileHelper.DynamicBuilder()
|
||||||
|
.Class("Roles", new TableAttribute() { Name = "T_Role" },
|
||||||
|
new IndexAttribute("Name_Index", "Name", false))
|
||||||
|
.Extend(typeof(BaseModel))
|
||||||
|
.Property("Id", typeof(int),
|
||||||
|
new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 })
|
||||||
|
.Property("Name", typeof(string),
|
||||||
|
new ColumnAttribute() { StringLength = 20, Position = 2 })
|
||||||
|
.Build();
|
||||||
|
var dict = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["Name"] = "系统管理员",
|
||||||
|
["UpdateTime"] = DateTime.Now,
|
||||||
|
["UpdatePerson"] = "admin"
|
||||||
|
};
|
||||||
|
var instance = DynamicCompileHelper.CreateObjectByType(type, dict);
|
||||||
|
//根据Type生成表
|
||||||
|
fsql.CodeFirst.SyncStructure(type);
|
||||||
|
fsql.Insert<object>().AsType(type).AppendData(instance).ExecuteAffrows();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class BaseModel
|
||||||
|
{
|
||||||
|
[Column(Position = 99)]
|
||||||
|
public DateTime UpdateTime
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Column(Position = 100, StringLength = 20)]
|
||||||
|
public string UpdatePerson
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -35,6 +35,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.DynamicEntity\FreeSql.Extensions.DynamicEntity.csproj" />
|
||||||
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.LazyLoading\FreeSql.Extensions.LazyLoading.csproj" />
|
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.LazyLoading\FreeSql.Extensions.LazyLoading.csproj" />
|
||||||
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.Linq\FreeSql.Extensions.Linq.csproj" />
|
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.Linq\FreeSql.Extensions.Linq.csproj" />
|
||||||
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
||||||
|
15
FreeSql.sln
15
FreeSql.sln
@ -125,6 +125,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Provider.Xugu", "Pr
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Tests.Provider.Xugu", "FreeSql.Tests\FreeSql.Tests.Provider.Xugu\FreeSql.Tests.Provider.Xugu.csproj", "{16C21D77-20AC-4722-AD97-F53BDDE8210C}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Tests.Provider.Xugu", "FreeSql.Tests\FreeSql.Tests.Provider.Xugu\FreeSql.Tests.Provider.Xugu.csproj", "{16C21D77-20AC-4722-AD97-F53BDDE8210C}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Extensions.DynamicEntity", "Extensions\FreeSql.Extensions.DynamicEntity\FreeSql.Extensions.DynamicEntity.csproj", "{FC4639EC-7787-4F85-AC02-26875E39E573}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -747,6 +749,18 @@ Global
|
|||||||
{16C21D77-20AC-4722-AD97-F53BDDE8210C}.Release|x64.Build.0 = Release|Any CPU
|
{16C21D77-20AC-4722-AD97-F53BDDE8210C}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{16C21D77-20AC-4722-AD97-F53BDDE8210C}.Release|x86.ActiveCfg = Release|Any CPU
|
{16C21D77-20AC-4722-AD97-F53BDDE8210C}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{16C21D77-20AC-4722-AD97-F53BDDE8210C}.Release|x86.Build.0 = Release|Any CPU
|
{16C21D77-20AC-4722-AD97-F53BDDE8210C}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@ -787,6 +801,7 @@ Global
|
|||||||
{8A06B18A-A8BF-4AEA-AFE4-0F573C2DBFEE} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
|
{8A06B18A-A8BF-4AEA-AFE4-0F573C2DBFEE} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
|
||||||
{71A6F937-D11B-4AE4-9933-BB6B4D925665} = {4A92E8A6-9A6D-41A1-9CDA-DE10899648AA}
|
{71A6F937-D11B-4AE4-9933-BB6B4D925665} = {4A92E8A6-9A6D-41A1-9CDA-DE10899648AA}
|
||||||
{8064870C-22EA-4A58-972D-DBD57D096D91} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
|
{8064870C-22EA-4A58-972D-DBD57D096D91} = {2A381C57-2697-427B-9F10-55DA11FD02E4}
|
||||||
|
{FC4639EC-7787-4F85-AC02-26875E39E573} = {4A92E8A6-9A6D-41A1-9CDA-DE10899648AA}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
RESX_NeutralResourcesLanguage = en-US
|
RESX_NeutralResourcesLanguage = en-US
|
||||||
|
@ -1301,17 +1301,4 @@ SELECT ");
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region DynamicEntity
|
|
||||||
#if net40 || NETSTANDARD2_0
|
|
||||||
#else
|
|
||||||
/// <summary>
|
|
||||||
/// 动态构建Class Type
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static DynamicCompileBuilder DynamicEntity(this ICodeFirst codeFirst, string className, TableAttribute tableAttribute)
|
|
||||||
{
|
|
||||||
return new DynamicCompileBuilder(className, tableAttribute);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
|
@ -4248,43 +4248,6 @@
|
|||||||
<param name="type"></param>
|
<param name="type"></param>
|
||||||
<returns>Dict:key=属性名,value=注释</returns>
|
<returns>Dict:key=属性名,value=注释</returns>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:FreeSql.Internal.DynamicCompileBuilder.#ctor(System.String,System.Attribute[])">
|
|
||||||
<summary>
|
|
||||||
配置Class
|
|
||||||
</summary>
|
|
||||||
<param name="className">类名</param>
|
|
||||||
<param name="attributes">类标记的特性[Table(Name = "xxx")]</param>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:FreeSql.Internal.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.Internal.DynamicCompileBuilder.Build">
|
|
||||||
<summary>
|
|
||||||
Emit动态创建出Class - Type
|
|
||||||
</summary>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:FreeSql.Internal.DynamicCompileBuilder.FirstCharToLower(System.String)">
|
|
||||||
<summary>
|
|
||||||
首字母小写
|
|
||||||
</summary>
|
|
||||||
<param name="input"></param>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:FreeSql.Internal.DynamicCompileBuilder.FirstCharToUpper(System.String)">
|
|
||||||
<summary>
|
|
||||||
首字母大写
|
|
||||||
</summary>
|
|
||||||
<param name="input"></param>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="P:FreeSql.Internal.DbUpdateVersionException.Table">
|
<member name="P:FreeSql.Internal.DbUpdateVersionException.Table">
|
||||||
<summary>
|
<summary>
|
||||||
更新实体的元数据
|
更新实体的元数据
|
||||||
@ -5906,12 +5869,6 @@
|
|||||||
<param name="source"></param>
|
<param name="source"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:FreeSqlGlobalExtensions.DynamicEntity(FreeSql.ICodeFirst,System.String,FreeSql.DataAnnotations.TableAttribute)">
|
|
||||||
<summary>
|
|
||||||
动态构建Class Type
|
|
||||||
</summary>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:System.Linq.Expressions.LambadaExpressionExtensions.And``1(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}},System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
|
<member name="M:System.Linq.Expressions.LambadaExpressionExtensions.And``1(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}},System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
|
||||||
<summary>
|
<summary>
|
||||||
使用 and 拼接两个 lambda 表达式
|
使用 and 拼接两个 lambda 表达式
|
||||||
|
Loading…
x
Reference in New Issue
Block a user