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
34 changes: 23 additions & 11 deletions Test/TestWeb/Components/Pages/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
@using System.Diagnostics.CodeAnalysis
@using LoggerProviderExtensions.HubLogger
@using System.Text
@using MT.Toolkit
@implements IAsyncDisposable
<PageTitle>Home</PageTitle>
<div style="height:500px;overflow-y:auto">
@logContent.ToString()
</div>
<button @onclick="@Log">日志测试</button>
<button @onclick="@DebounceTest">Debounce</button>
<button @onclick="@ThrottleTest">Throttle</button>
Welcome to your new app.
@code {
[Inject, NotNull] ILogger<Home>? Logger { get; set; }
Expand All @@ -18,33 +21,42 @@ Welcome to your new app.
base.OnInitialized();
// _ = StartFetchLog();
Channel.OnLog += HandleLog;
debounce = Debounce<string>.Create(Log2, 1000);
throttle = Throttle<string>.Create(Log2, 1000);
}

private void HandleLog(LoggerProviderExtensions.StructuredLogEntry entry)
{
logContent.AppendLine($"{entry.Category}: {entry.MessageTemplate}");
Console.WriteLine($"{entry.Category}: {entry.MessageTemplate}");
InvokeAsync(StateHasChanged);
}

// private async Task StartFetchLog()
// {
// await foreach (var item in Channel.LogEntries)
// {
// logContent.AppendLine($"{item.Category}: {item.MessageTemplate}");
// Console.WriteLine($"{item.Category}: {item.MessageTemplate}");
// StateHasChanged();
// }
// }

private void Log()
{
Logger.LogInformation("This is a log message at {Time}", DateTime.Now);
}
private void Log2(string type)
{
Logger.LogInformation("{type} log at {Time}", type, DateTime.Now);
}

private Debounce<string>? debounce;
private Throttle<string>? throttle;

private async Task DebounceTest()
{
_ = debounce!.InvokeAsync("DebounceTest");
}
private async Task ThrottleTest()
{
_ = throttle!.InvokeAsync("ThrottleTest");
}

public ValueTask DisposeAsync()
{
Channel.OnLog -= HandleLog;
debounce!.Dispose();
throttle!.Dispose();
return ValueTask.CompletedTask;
}
}
3 changes: 3 additions & 0 deletions doc/更新日志.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 版本功能更新日志

### v2026.06.02.1
- ⚡️支持AOT编译

### v2.1.9.0
- ⚡️移除Mapper相关代码 -> 使用`AutoGenMapperGenerator`代替

Expand Down
1 change: 1 addition & 0 deletions src/SoapRequestHelper/SoapRequestHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReadmeFile>nuget.md</PackageReadmeFile>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
Expand Down
63 changes: 50 additions & 13 deletions src/SoapRequestHelper/SoapResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ private static DataTable CreateDataTable(IEnumerable<XElement> schema, IEnumerab
return dt;
}

#if NET8_0_OR_GREATER
[return: System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)]
#endif
private static Type ConvertElementType(string type)
{
return type switch
Expand All @@ -245,10 +248,10 @@ private static Type ConvertElementType(string type)
_ => typeof(string),
};
}
static readonly ConcurrentDictionary<Type, MethodInfo?> tryParseCache = [];

private static object? Parse(string? input, Type type)
{
if ( string.IsNullOrEmpty(input))
if (string.IsNullOrEmpty(input))
{
return default;
}
Expand All @@ -257,20 +260,54 @@ private static Type ConvertElementType(string type)
return input.Trim();
}
type = Nullable.GetUnderlyingType(type) ?? type;
var method = tryParseCache.GetOrAdd(type, t =>
{
return t.GetMethod("TryParse", [typeof(string), t.MakeByRefType()]);
});
if (method == null)

if (TryParse(input, type, out var value))
{
return default;
return value;
}
object?[] parameters = [input, default];
var r = method.Invoke(null, parameters);
if (r is bool b && b == true)

return default;

static bool TryParse(string text, Type type, out object? value)
{
return parameters[1];
if (type == typeof(DateTime))
{
_ = DateTime.TryParse(text, out var v);
value = v;
return true;
}
else if (type == typeof(bool))
{
_ = bool.TryParse(text, out var v);
value = v;
return true;
}
else if (type == typeof(int))
{
_ = int.TryParse(text, out var v);
value = v;
return true;
}
else if (type == typeof(decimal))
{
_ = decimal.TryParse(text, out var v);
value = v;
return true;
}
else if (type == typeof(long))
{
_ = long.TryParse(text, out var v);
value = v;
return true;
}
else if (type == typeof(short))
{
_ = short.TryParse(text, out var v);
value = v;
return true;
}
value = default;
return false;
}
return default;
}
}
131 changes: 68 additions & 63 deletions src/Toolkit/DataTableExtension/DataTableBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,76 +1,81 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;

namespace MT.Toolkit.DataTableExtension
namespace MT.Toolkit.DataTableExtension;

internal static class DataTableBuilder<
#if NET8_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
T>
{
public static class DataTableBuilder<T>
{
private static Func<DataRow, object>? func;
private static Func<DataRow, object>? func;

public static Func<DataRow, object> Build(DataColumnCollection dataColumn, bool mapAll)
{
if (func != null) return func;
func = CreateFunc(typeof(T), dataColumn, mapAll);
return func;
}
public static Func<DataRow, object> Build(DataColumnCollection dataColumn, bool mapAll)
{
if (func != null) return func;
func = CreateFunc(dataColumn, mapAll);
return func;
}

static Func<DataRow, object> CreateFunc(Type tarType, DataColumnCollection cols, bool mapAll)
{
// Datarow row;
ParameterExpression rowExp = Expression.Parameter(typeof(DataRow), "row");
List<MemberBinding> bindings = new List<MemberBinding>();
// fields
var fields = tarType.GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo field in fields)
{
void work()
{
if (cols.Contains(field.Name))
{
DataColumn col = cols[field.Name]!;
var valueExp = TableExpressionBase.GetTargetValueExpression(col, rowExp, field.FieldType);
MemberAssignment memberAssignment = Expression.Bind(field, valueExp);
bindings.Add(memberAssignment);
return;
}
if (mapAll)
{
throw new ArgumentException($"Property {field.Name} is not matched by any column in datatable");
}
}
work();
}
static Func<DataRow, object> CreateFunc(DataColumnCollection cols, bool mapAll)
{
Type tarType = typeof(T);
// Datarow row;
ParameterExpression rowExp = Expression.Parameter(typeof(DataRow), "row");
List<MemberBinding> bindings = [];
// fields
var fields = tarType.GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo field in fields)
{
void work()
{
if (cols.Contains(field.Name))
{
DataColumn col = cols[field.Name]!;
var valueExp = TableExpressionBase.GetTargetValueExpression(col, rowExp, field.FieldType);
MemberAssignment memberAssignment = Expression.Bind(field, valueExp);
bindings.Add(memberAssignment);
return;
}
if (mapAll)
{
throw new ArgumentException($"Property {field.Name} is not matched by any column in datatable");
}
}
work();
}

// properties
var props = tarType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in props)
{
void work()
{
if (cols.Contains(prop.Name))
{
if (!prop.CanWrite) return;
DataColumn col = cols[prop.Name]!;
var valueExp = TableExpressionBase.GetTargetValueExpression(col, rowExp, prop.PropertyType);
MemberAssignment memberAssignment = Expression.Bind(prop, valueExp);
bindings.Add(memberAssignment);
return;
}
if (mapAll)
{
throw new ArgumentException($"Property {prop.Name} is not matched by any column in datatable");
}
// properties
var props = tarType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in props)
{
void work()
{
if (cols.Contains(prop.Name))
{
if (!prop.CanWrite) return;
DataColumn col = cols[prop.Name]!;
var valueExp = TableExpressionBase.GetTargetValueExpression(col, rowExp, prop.PropertyType);
MemberAssignment memberAssignment = Expression.Bind(prop, valueExp);
bindings.Add(memberAssignment);
return;
}
if (mapAll)
{
throw new ArgumentException($"Property {prop.Name} is not matched by any column in datatable");
}

}
work();
}
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(tarType), bindings);
var lambda = Expression.Lambda<Func<DataRow, object>>(memberInitExpression, rowExp);
return lambda.Compile();
}
}
work();
}
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(tarType), bindings);
var lambda = Expression.Lambda<Func<DataRow, object>>(memberInitExpression, rowExp);
return lambda.Compile();
}

}
}
Loading
Loading