mirror of
https://gitee.com/wang-yin1/wpf-visual-process-framework
synced 2026-03-02 15:50:51 +08:00
55 lines
2.0 KiB
C#
55 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace VisionFrame.Base.Converter
|
|
{
|
|
public class NodeJsonConverter : JsonConverter<NodeModelBase>
|
|
{
|
|
class TypeInfoTemp
|
|
{
|
|
public string TypeInfo { get; set; }
|
|
}
|
|
// 字符串到对象实例的转换过程
|
|
public override NodeModelBase? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
string json = reader.GetString();// 包含程序信息
|
|
|
|
TypeInfoTemp ti = System.Text.Json.JsonSerializer.Deserialize<TypeInfoTemp>(json);
|
|
string[] type_info = ti.TypeInfo.Split(";");
|
|
Assembly assembly = Assembly.GetEntryAssembly();// 取当前执行的程序集
|
|
if (assembly.GetName().Name != type_info[0])
|
|
{
|
|
assembly = Assembly.LoadFile(Environment.CurrentDirectory + "/Activities/" + type_info[0] + ".dll");
|
|
}
|
|
// 最终要执的节点Model
|
|
Type type = assembly.GetType(type_info[1]);
|
|
|
|
NodeModelBase node = (NodeModelBase)System.Text.Json.JsonSerializer.Deserialize(json, type);
|
|
return node;
|
|
}
|
|
|
|
// 对象到Json字符串的转换过程
|
|
public override void Write(Utf8JsonWriter writer, NodeModelBase value, JsonSerializerOptions options)
|
|
{
|
|
// value--->Json
|
|
// writer--写
|
|
// 准备类型的记录
|
|
string type = value.GetType().FullName;
|
|
string assembly = value.GetType().Assembly.GetName().Name;
|
|
value.TypeInfo = assembly + ";" + type;
|
|
|
|
// 如果直接放value 会循环触发这个方法
|
|
// 用一个动态变量接收,
|
|
dynamic temp = value;
|
|
string json = System.Text.Json.JsonSerializer.Serialize(temp);
|
|
writer.WriteStringValue(json);
|
|
}
|
|
}
|
|
}
|