mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-12-29 18:04:42 +08:00
fix: 修复 XmlSerializer 潜在的内存泄漏问题
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
@@ -9,6 +10,9 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities
|
||||
{
|
||||
internal static class XmlUtility
|
||||
{
|
||||
// REF: https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.serialization.xmlserializer#dynamically-generated-assemblies
|
||||
private static Hashtable _serializers = new Hashtable();
|
||||
|
||||
public static string Serialize(Type type, object obj)
|
||||
{
|
||||
string xml;
|
||||
@@ -20,10 +24,16 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities
|
||||
settings.WriteEndDocumentOnClose = false;
|
||||
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
|
||||
|
||||
string skey = type.AssemblyQualifiedName;
|
||||
XmlSerializer? xmlSerializer = (XmlSerializer)_serializers[skey];
|
||||
if (xmlSerializer == null)
|
||||
{
|
||||
xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
||||
_serializers[skey] = xmlSerializer;
|
||||
}
|
||||
|
||||
using var stream = new MemoryStream();
|
||||
using var writer = XmlWriter.Create(stream, settings);
|
||||
|
||||
XmlSerializer xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
||||
XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
|
||||
xmlNamespace.Add(string.Empty, string.Empty);
|
||||
xmlSerializer.Serialize(writer, obj, xmlNamespace);
|
||||
|
||||
Reference in New Issue
Block a user