From 86bdbb6787b1f948615d8ee893ee579b7049dd63 Mon Sep 17 00:00:00 2001 From: zengwenjie <1663900244@qq.com> Date: Fri, 19 Sep 2025 21:15:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86TryDecode=E7=9A=84=E5=8F=AF?= =?UTF-8?q?=E7=A9=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Deedy.Activity/Helpers/ElementHelper.cs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/DeedyDesigner/Deedy.Activity/Helpers/ElementHelper.cs b/DeedyDesigner/Deedy.Activity/Helpers/ElementHelper.cs index 4432ad4..187fbff 100644 --- a/DeedyDesigner/Deedy.Activity/Helpers/ElementHelper.cs +++ b/DeedyDesigner/Deedy.Activity/Helpers/ElementHelper.cs @@ -237,5 +237,58 @@ namespace Deedy.Activity catch { document = string.Empty; } return result; } + /// + /// 尝试将一个字符串「档案」解析为「IElement」对象 + /// + /// 要解析的「档案」 + /// 解析后的对象 + /// 如果解析失败则返回「False」 + public static bool TryDecode(this string document, out TReturn element) where TReturn : IElement + { +#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。 + TReturn instance = default; +#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。 + bool result = false; + try + { + if (XamlReader.Parse(document) is TReturn @return) + { + instance = @return; + result = true; + } + } + catch { } +#pragma warning disable CS8601 // 引用类型赋值可能为 null。 + element = instance; +#pragma warning restore CS8601 // 引用类型赋值可能为 null。 + return result; + } + /// + /// 尝试将一个字符串「档案」解析为「IElement」对象 + /// + /// 要解析的「档案」 + /// 解析后的对象 + /// 如果解析失败则返回「False」 + public static bool TryDecode(this string document,[AllowNull] out IElement element) + { + +#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。 + IElement instance = null; +#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。 + bool result = false; + try + { + if (XamlReader.Parse(document) is IElement @return) + { + instance = @return; + result = true; + } + } + catch { } +#pragma warning disable CS8601 // 引用类型赋值可能为 null。 + element = instance; +#pragma warning restore CS8601 // 引用类型赋值可能为 null。 + return result; + } } }