48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Bunny.Common.Utils.Net;
|
|
|
|
public static partial class NetUtil
|
|
{
|
|
/// <summary>
|
|
/// 仿写java对象拷贝
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="target"></param>
|
|
/// <typeparam name="T1"></typeparam>
|
|
/// <typeparam name="T2"></typeparam>
|
|
public static void CopyProperties<T1, T2>(T1 source, T2 target)
|
|
{
|
|
var sourceProperties = source!.GetType().GetProperties();
|
|
var targetProperties = target!.GetType().GetProperties();
|
|
|
|
foreach (var sourceProperty in sourceProperties)
|
|
{
|
|
var targetProperty = targetProperties.FirstOrDefault(x =>
|
|
x.Name == sourceProperty.Name && x.PropertyType == sourceProperty.PropertyType);
|
|
|
|
if (targetProperty == null || !targetProperty.CanWrite) continue;
|
|
var value = sourceProperty.GetValue(source);
|
|
targetProperty.SetValue(target, value);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取token
|
|
/// </summary>
|
|
/// <param name="request">请求</param>
|
|
/// <returns>token值</returns>
|
|
public static string? GetToken(HttpRequest request)
|
|
{
|
|
return request.Headers["token"];
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取请求路径
|
|
/// </summary>
|
|
/// <param name="request">请求</param>
|
|
public static string? GetRequestPath(HttpRequest request)
|
|
{
|
|
return request.Path.Value;
|
|
}
|
|
} |