Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>9.0.2</Version>
<Version>9.0.3</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ protected virtual bool Parse(ReadOnlyMemory<byte> data, TEntity entity)
?? GetPropertyConverterAttribute(p);
if (attr != null)
{
p.SetValue(entity, attr.ConvertTo(data));
var v = attr.ConvertTo(data);
if (v?.GetType() == attr.Type)
Comment thread
ArgoZhang marked this conversation as resolved.
Outdated
{
p.SetValue(entity, v);
}
}
}
ret = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using System.Reflection;

namespace System;

/// <summary>
/// Activator 扩展方法
/// </summary>
public static class ActivatorExtesnions
Comment thread
ArgoZhang marked this conversation as resolved.
Outdated
Comment thread
ArgoZhang marked this conversation as resolved.
Outdated
{
/// <summary>
/// 通过指定类型与参数创建实例方法
/// </summary>
/// <param name="type"></param>
/// <param name="args"></param>
/// <returns></returns>
public static object? CreateInstance(this Type type, object?[]? args = null)
{
var bindings = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default;
Comment thread
ArgoZhang marked this conversation as resolved.
Outdated
return Activator.CreateInstance(type, bindings, null, args, null);
}

/// <summary>
/// 通过指定类型与参数创建实例方法
/// </summary>
/// <param name="type"></param>
/// <param name="args"></param>
/// <returns></returns>
public static TType? CreateInstance<TType>(this Type type, object?[]? args = null)
{
TType? ret = default;
var value = type.CreateInstance(args);
if (value is TType v)
{
ret = v;
}
return ret;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

Expand All @@ -18,11 +18,7 @@ static class DataPropertyExtensions
if (converterType != null)
{
var converterParameters = attribute.ConverterParameters;
var c = Activator.CreateInstance(converterType, converterParameters);
if (c is IDataPropertyConverter v)
{
converter = v;
}
converter = converterType.CreateInstance<IDataPropertyConverter>(converterParameters);
}
return converter;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>9.0.1</Version>
<Version>9.0.2</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,7 @@ public static void SetDataPackageAdapter<TEntity>(this ITcpSocketClient client,
if (converterType is { Type: not null })
{
// 如果类型上有 SocketDataTypeConverterAttribute 特性则使用特性中指定的转换器
if (Activator.CreateInstance(converterType.Type) is IDataConverter<TEntity> socketDataConverter)
{
converter = socketDataConverter;
}
converter = converterType.Type.CreateInstance<IDataConverter<TEntity>>();
}
else
{
Expand Down
57 changes: 56 additions & 1 deletion test/UnitTestTcpSocket/TcpSocketFactoryTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

Expand Down Expand Up @@ -916,6 +916,61 @@ Task ReceivedEntityCallBack(MockEntity? entity)
}
}

[Fact]
public async Task Convert_Ok()
{
var port = 8899;
var server = StartTcpServer(port, MockSplitPackageAsync);

var client = CreateClient();
var tcs = new TaskCompletionSource();
var receivedBuffer = new byte[128];
MockConverterEntity? entity = null;

// 连接 TCP Server
var connect = await client.ConnectAsync("localhost", port);

client.SetDataPackageAdapter<MockConverterEntity>(new FixLengthDataPackageHandler(7), ReceivedCallBack);

var data = new ReadOnlyMemory<byte>([1, 2, 3, 4, 5]);
await client.SendAsync(data);

// 等待接收数据处理完成
await tcs.Task;

// 验证实体类不为空
Assert.NotNull(entity);
Assert.Equal("3.14", entity.Value1.ToString("#.##"));
Comment thread
ArgoZhang marked this conversation as resolved.

Task ReceivedCallBack(MockConverterEntity? data)
{
entity = data;
tcs.SetResult();
return Task.CompletedTask;
}
}

[DataTypeConverter(Type = typeof(DataConverter<MockConverterEntity>))]
class MockConverterEntity
{
[DataPropertyConverter(Type = typeof(byte[]), Offset = 0, Length = 5)]
public byte[]? Header { get; set; }

[DataPropertyConverter(Type = typeof(byte[]), Offset = 5, Length = 2)]
public byte[]? Body { get; set; }

[DataPropertyConverter(Type = typeof(float), Offset = 5, Length = 1, ConverterType = typeof(FloatConverter), ConverterParameters = [0.01f])]
public float Value1 { get; set; }
}

class FloatConverter(float rate) : IDataPropertyConverter
{
public object? Convert(ReadOnlyMemory<byte> data)
{
return (float)Math.Round(314 * rate, 2);
}
}

private static TcpListener StartTcpServer(int port, Func<TcpClient, Task> handler)
{
var server = new TcpListener(IPAddress.Loopback, port);
Expand Down
2 changes: 1 addition & 1 deletion test/UnitTestTcpSocket/TcpSocketPropertyConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ public void DoubleConverter_Ok()
{
var converter = new DataDoubleLittleEndianConverter();
var actual = converter.Convert(new byte[] { 0x1F, 0x85, 0xEB, 0x51, 0xB8, 0x1E, 0x09, 0x40 });
Assert.Equal((double)3.14, actual);
Assert.Equal(3.14, actual);
}
}