Files
KinginfoGateway/src/Foundation/ThingsGateway.Foundation/OperResult/OperResultExtension.cs
Diego c94308454f build: 10.7.42
feat:更改后台服务多语言处理方式
2025-06-10 15:49:47 +08:00

81 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://thingsgateway.cn/
// QQ群605534569
//------------------------------------------------------------------------------
namespace ThingsGateway.Foundation;
/// <summary>
/// OperResultExtensions
/// </summary>
public static class OperResultExtension
{
/// <summary>
/// 转换对应类型
/// </summary>
public static OperResult<TResult> GetResultFromBytes<TResult>(this OperResult<byte[]> result, Func<byte[]?, TResult> translator)
{
try
{
return result.IsSuccess ? new() { Content = translator(result.Content) } : new OperResult<TResult>(result);
}
catch (Exception ex)
{
return new OperResult<TResult>(string.Format(AppResource.TransBytesError, result.Content?.ToHexString(' '), result.Content?.Length), ex);
}
}
/// <summary>
/// 操作成功则继续,并返回对应结果值
/// </summary>
public static OperResult<T1> OperResultFrom<T1>(this OperResult result, Func<T1> func)
{
if (result.IsSuccess)
return new OperResult<T1>() { Content = func() };
else
return new OperResult<T1>(result);
}
/// <summary>
/// 操作成功则继续,并返回对应结果值
/// </summary>
public static OperResult<T1> OperResultFrom<T1, T2>(this OperResult<T2> result, Func<T1> func)
{
if (result.IsSuccess)
return new OperResult<T1>() { Content = func() };
else
return new OperResult<T1>(result);
}
/// <summary>
/// 操作成功则继续
/// </summary>
public static OperResult Then(this OperResult result, Func<OperResult> func)
{
return !result.IsSuccess ? result : func();
}
/// <inheritdoc cref="Then(OperResult, Func{OperResult})"/>
public static OperResult<T> Then<T>(this OperResult result, Func<OperResult<T>> func)
{
return !result.IsSuccess ? new OperResult<T>(result) : func();
}
/// <inheritdoc cref="Then(OperResult, Func{OperResult})"/>
public static OperResult<T2> Then<T, T2>(this OperResult<T> result, Func<T?, OperResult<T2>> func)
{
return !result.IsSuccess ? new OperResult<T2>(result) : func(result.Content);
}
/// <inheritdoc cref="Then(OperResult, Func{OperResult})"/>
public static OperResult<T, T2> Then<T, T2>(this OperResult result, Func<OperResult<T, T2>> func)
{
return !result.IsSuccess ? new OperResult<T, T2>(result) : func();
}
}