mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-04-05 15:56:33 +08:00
暂停更新 Workbench(WPF)项目
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
@@ -14,7 +15,43 @@ namespace Serein.Library.Utils
|
||||
public static class ConvertHelper
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 对象转JSON文本
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToJsonText(this object obj)
|
||||
{
|
||||
var jsonText = JsonConvert.SerializeObject(obj, Formatting.Indented);
|
||||
return jsonText;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// JSON文本转对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T">转换类型</typeparam>
|
||||
/// <param name="json">JSON文本</param>
|
||||
/// <returns></returns>
|
||||
public static T ToJsonObject<T>(this string json)
|
||||
{
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对象转换(好像没啥用)
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult"></typeparam>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public static TResult ToConvert<TResult>(this object data)
|
||||
{
|
||||
var type = typeof(TResult);
|
||||
@@ -25,6 +62,14 @@ namespace Serein.Library.Utils
|
||||
return (TResult)data.ToConvert(type);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对象转换(好像没啥用)
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static object ToConvert(this object data, Type type)
|
||||
{
|
||||
if (type.IsValueType)
|
||||
@@ -44,12 +89,24 @@ namespace Serein.Library.Utils
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 文本
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static T ValueParse<T>(object value) where T : struct, IComparable<T>
|
||||
{
|
||||
string valueStr = value.ToString();
|
||||
return valueStr.ToValueData<T>() ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文本转换数值
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static object ValueParse(Type type, object value)
|
||||
{
|
||||
string valueStr = value.ToString();
|
||||
@@ -57,7 +114,13 @@ namespace Serein.Library.Utils
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 文本转换值对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="valueStr"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public static T ToValueData<T>(this string valueStr) where T : struct, IComparable<T>
|
||||
{
|
||||
if (string.IsNullOrEmpty(valueStr))
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -7,8 +10,13 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// 对象转换工具类
|
||||
/// </summary>
|
||||
public static class ObjectConvertHelper
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 父类转为子类
|
||||
/// </summary>
|
||||
|
||||
@@ -15,7 +15,8 @@ namespace Serein.Library.Utils
|
||||
/// </summary>
|
||||
public class UIContextOperation
|
||||
{
|
||||
private readonly SynchronizationContext context;
|
||||
private SynchronizationContext context;
|
||||
private readonly Func<SynchronizationContext> getUiContext = null;
|
||||
|
||||
static UIContextOperation()
|
||||
{
|
||||
@@ -42,12 +43,25 @@ namespace Serein.Library.Utils
|
||||
this.context = synchronizationContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 传入UI线程上下文
|
||||
/// </summary>
|
||||
/// <param name="synchronizationContext">线程上下文</param>
|
||||
public UIContextOperation(Func<SynchronizationContext> getUiContext)
|
||||
{
|
||||
this.getUiContext = getUiContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步方式进行调用方法
|
||||
/// </summary>
|
||||
/// <param name="uiAction">要执行的UI操作</param>
|
||||
public void Invoke(Action uiAction)
|
||||
{
|
||||
if(context is null && getUiContext != null)
|
||||
{
|
||||
context = getUiContext.Invoke();
|
||||
}
|
||||
context?.Post(state =>
|
||||
{
|
||||
uiAction?.Invoke();
|
||||
@@ -61,6 +75,10 @@ namespace Serein.Library.Utils
|
||||
/// <returns></returns>
|
||||
public Task InvokeAsync(Action uiAction)
|
||||
{
|
||||
if (context is null && getUiContext != null)
|
||||
{
|
||||
context = getUiContext.Invoke();
|
||||
}
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
|
||||
context?.Post(state =>
|
||||
|
||||
Reference in New Issue
Block a user