Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,37 +75,16 @@ public DataTableDynamicContext(DataTable table, Action<DataTableDynamicContext,
{
DataTable = table;
AddAttributesCallback = addAttributesCallback;
OnValueChanged = OnCellValueChanged;

// 获得 DataTable 列信息转换为 ITableColumn 集合
var cols = InternalGetColumns();

// Emit 生成动态类
DynamicObjectType = CreateType();
// 生成动态类型 DataTableDynamicObjectType 继承 DataTableDynamicObject 并添加属性
DynamicObjectType = EmitHelper.CreateTypeByName($"BootstrapBlazor_{nameof(DataTableDynamicContext)}_{GetHashCode()}", cols, typeof(DataTableDynamicObject), OnColumnCreating);

// 获得显示列
Columns = Utility.GetTableColumns(DynamicObjectType, cols).Where(col => GetShownColumns(col, invisibleColumns, shownColumns, hiddenColumns));

OnValueChanged = OnCellValueChanged;

[ExcludeFromCodeCoverage]
Type CreateType()
{
// Emit 生成动态类 (使用缓存)
var columnNames = string.Join('|', table.Columns.Cast<DataColumn>().Select(static c => $"{c.ColumnName}:{c.DataType.FullName}"));
var cacheKey = $"BootstrapBlazor-{nameof(DataTableDynamicContext)}-{columnNames}";
var dynamicType = CacheManager.GetDynamicObjectTypeByName(cacheKey, cols, OnColumnCreating, out var cached);

// 缓存命中时仍需调用回调以处理列属性
if (cached && AddAttributesCallback != null)
{
foreach (var col in cols)
{
AddAttributesCallback?.Invoke(this, col);
}
}

return dynamicType ?? throw new InvalidOperationException();
}
}

private static bool GetShownColumns(ITableColumn col, IEnumerable<string>? invisibleColumns, IEnumerable<string>? shownColumns, IEnumerable<string>? hiddenColumns)
Expand Down
15 changes: 0 additions & 15 deletions src/BootstrapBlazor/Services/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;

#if NET8_0_OR_GREATER
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -765,18 +764,4 @@ public static object GetFormatterInvoker(Type type, Func<object, Task<string?>>

private static Func<TType, Task<string?>> InvokeFormatterAsync<TType>(Func<object?, Task<string?>> formatter) => new(v => formatter(v));
#endregion

internal static Type? GetDynamicObjectTypeByName(string key, IEnumerable<ITableColumn> cols, Func<ITableColumn, IEnumerable<CustomAttributeBuilder>>? creatingCallback, out bool cached)
{
var created = false;
var type = Instance.GetOrCreate(key, _ =>
{
created = true;
return EmitHelper.CreateTypeByName($"BootstrapBlazor_{nameof(DataTableDynamicContext)}_{key}", cols, typeof(DataTableDynamicObject), creatingCallback);
});

// 是否从缓存中获取到的值
cached = !created;
return type;
}
}
6 changes: 3 additions & 3 deletions src/BootstrapBlazor/Utils/EmitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public static class EmitHelper
/// <para lang="zh">回调委托</para>
/// <para lang="en">Callback delegate</para>
/// </param>
public static Type? CreateTypeByName(string typeName, IEnumerable<ITableColumn> cols, Type? parent = null, Func<ITableColumn, IEnumerable<CustomAttributeBuilder>>? creatingCallback = null)
public static Type CreateTypeByName(string typeName, IEnumerable<ITableColumn> cols, Type? parent = null, Func<ITableColumn, IEnumerable<CustomAttributeBuilder>>? creatingCallback = null)
{
var typeBuilder = CreateTypeBuilderByName(typeName, parent);

foreach (var col in cols)
{
var attributeBuilds = creatingCallback?.Invoke(col);
typeBuilder.CreateProperty(col, attributeBuilds);
}
return typeBuilder.CreateType();

return typeBuilder.CreateType()!;
}

private static TypeBuilder CreateTypeBuilderByName(string typeName, Type? parent = null)
Expand Down
Loading