Skip to content

Commit

Permalink
Merge pull request #1740 from vsuyi/master
Browse files Browse the repository at this point in the history
DynamicEntity.DynamicCompileBuilder增加TypeBuilder只读属性和仅构建类型的方法
  • Loading branch information
2881099 authored Mar 4, 2024
2 parents ad77185 + 1cec907 commit 6c10057
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions FreeSql/Extensions/DynamicEntityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class DynamicCompileBuilder
private List<DynamicPropertyInfo> _properties = new List<DynamicPropertyInfo>();
private Type _superClass = null;
private IFreeSql _fsql = null;
private TypeBuilder _typeBuilder = null;

/// <summary>
/// 配置Class
Expand All @@ -99,8 +100,25 @@ public DynamicCompileBuilder(IFreeSql fsql, string className, params Attribute[]
_fsql = fsql;
_className = className;
_tableAttributes = attributes;

//初始化AssemblyName的一个实例
var assemblyName = new AssemblyName("FreeSql.DynamicCompileBuilder");
//设置程序集的名称
var defineDynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
//动态在程序集内创建一个模块
var defineDynamicModule =
defineDynamicAssembly.DefineDynamicModule("FreeSql.DynamicCompileBuilder.Dynamics");

//动态的在模块内创建一个类
_typeBuilder =
defineDynamicModule.DefineType(_className, TypeAttributes.Public | TypeAttributes.Class, _superClass);
}

/// <summary>
/// 获取类型构建器,可作为要构建的Type来引用
/// </summary>
public TypeBuilder TypeBuilder { get { return _typeBuilder; } }

/// <summary>
/// 配置属性
/// </summary>
Expand Down Expand Up @@ -388,27 +406,23 @@ private void OverrideProperty(ref TypeBuilder typeBuilder, MethodBuilder methodB
/// <returns></returns>
public TableInfo Build()
{
//初始化AssemblyName的一个实例
var assemblyName = new AssemblyName("FreeSql.DynamicCompileBuilder");
//设置程序集的名称
var defineDynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
//动态在程序集内创建一个模块
var defineDynamicModule =
defineDynamicAssembly.DefineDynamicModule("FreeSql.DynamicCompileBuilder.Dynamics");
//动态的在模块内创建一个类
var typeBuilder =
defineDynamicModule.DefineType(_className, TypeAttributes.Public | TypeAttributes.Class, _superClass);
return _fsql.CodeFirst.GetTableByEntity(BuildJustType());
}

/// <summary>
/// Emit动态创建出Class - Type,不附带获取TableInfo
/// </summary>
/// <returns></returns>
public Type BuildJustType()
{
//设置TableAttribute
SetTableAttribute(ref typeBuilder);
SetTableAttribute(ref _typeBuilder);

//设置属性
SetPropertys(ref typeBuilder);
SetPropertys(ref _typeBuilder);

//创建类的Type对象
var type = typeBuilder.CreateTypeInfo().AsType();

return _fsql.CodeFirst.GetTableByEntity(type);
return _typeBuilder.CreateTypeInfo().AsType();
}

/// <summary>
Expand Down

0 comments on commit 6c10057

Please sign in to comment.