Skip to content

Commit 5caf279

Browse files
committed
refactor: 使用扩展方法重构代码
1 parent 310cff4 commit 5caf279

3 files changed

Lines changed: 45 additions & 10 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using System.Reflection;
6+
7+
namespace System;
8+
9+
/// <summary>
10+
/// Activator 扩展方法
11+
/// </summary>
12+
public static class ActivatorExtesnions
13+
{
14+
/// <summary>
15+
/// 通过指定类型与参数创建实例方法
16+
/// </summary>
17+
/// <param name="type"></param>
18+
/// <param name="args"></param>
19+
/// <returns></returns>
20+
public static object? CreateInstance(this Type type, object?[]? args = null)
21+
{
22+
var bindings = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default;
23+
return Activator.CreateInstance(type, bindings, null, args, null);
24+
}
25+
26+
/// <summary>
27+
/// 通过指定类型与参数创建实例方法
28+
/// </summary>
29+
/// <param name="type"></param>
30+
/// <param name="args"></param>
31+
/// <returns></returns>
32+
public static TType? CreateInstance<TType>(this Type type, object?[]? args = null)
33+
{
34+
TType? ret = default;
35+
var value = type.CreateInstance(args);
36+
if (value is TType v)
37+
{
38+
ret = v;
39+
}
40+
return ret;
41+
}
42+
}

src/extensions/BootstrapBlazor.Socket/Extensions/DataPropertyExtensions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

@@ -18,11 +18,7 @@ static class DataPropertyExtensions
1818
if (converterType != null)
1919
{
2020
var converterParameters = attribute.ConverterParameters;
21-
var c = Activator.CreateInstance(converterType, converterParameters);
22-
if (c is IDataPropertyConverter v)
23-
{
24-
converter = v;
25-
}
21+
converter = converterType.CreateInstance<IDataPropertyConverter>(converterParameters);
2622
}
2723
return converter;
2824
}

src/extensions/BootstrapBlazor.TcpSocket/Extensions/ITcpSocketClientExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,7 @@ public static void SetDataPackageAdapter<TEntity>(this ITcpSocketClient client,
242242
if (converterType is { Type: not null })
243243
{
244244
// 如果类型上有 SocketDataTypeConverterAttribute 特性则使用特性中指定的转换器
245-
if (Activator.CreateInstance(converterType.Type) is IDataConverter<TEntity> socketDataConverter)
246-
{
247-
converter = socketDataConverter;
248-
}
245+
converter = converterType.Type.CreateInstance<IDataConverter<TEntity>>();
249246
}
250247
else
251248
{

0 commit comments

Comments
 (0)