处理TryDecode的可空问题

This commit is contained in:
zengwenjie
2025-09-19 21:15:41 +08:00
parent e3e87256b0
commit 86bdbb6787

View File

@@ -237,5 +237,58 @@ namespace Deedy.Activity
catch { document = string.Empty; } catch { document = string.Empty; }
return result; return result;
} }
/// <summary>
/// 尝试将一个字符串「档案」解析为「IElement」对象
/// </summary>
/// <param name="document">要解析的「档案」</param>
/// <param name="element">解析后的对象</param>
/// <returns>如果解析失败则返回「False」</returns>
public static bool TryDecode<TReturn>(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;
}
/// <summary>
/// 尝试将一个字符串「档案」解析为「IElement」对象
/// </summary>
/// <param name="document">要解析的「档案」</param>
/// <param name="element">解析后的对象</param>
/// <returns>如果解析失败则返回「False」</returns>
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;
}
} }
} }