mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 11:24:46 +00:00
79 lines
2.1 KiB
C#
79 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace BehaviorTreeSlayer
|
|
{
|
|
public class XmlUtils
|
|
{
|
|
public static T DeSerialize<T>(string path, string name)
|
|
{
|
|
FileStream fileStream = File.Open(path + "/" + name, FileMode.Open);
|
|
XmlSerializer xml = new XmlSerializer(typeof(T));
|
|
T list;
|
|
try
|
|
{
|
|
list = (T)xml.Deserialize(fileStream);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
fileStream.Close();
|
|
}
|
|
return list;
|
|
}
|
|
public static T DeSerialize<T>(string text)
|
|
{
|
|
using (StringReader sr = new StringReader(text))
|
|
{
|
|
XmlSerializer xs = new XmlSerializer(typeof(T), Types);
|
|
return (T)xs.Deserialize(sr);
|
|
}
|
|
}
|
|
public static void XmlWriter<T>(T obj, string path)
|
|
{
|
|
using (FileStream fs = new FileStream(path, FileMode.Create))
|
|
{
|
|
XmlSerializer xml = new XmlSerializer(obj.GetType(), Types);
|
|
xml.Serialize(fs, obj);
|
|
}
|
|
}
|
|
static Type[] types;
|
|
|
|
public static Type[] Types
|
|
{
|
|
get
|
|
{
|
|
if (types == null)
|
|
{
|
|
types = AppDomain.CurrentDomain.GetAssemblies()
|
|
.SelectMany(a => a.GetTypes().Where(t => t.IsSubclassOf(typeof(TreeNode))))
|
|
.ToArray();
|
|
}
|
|
return types;
|
|
}
|
|
}
|
|
|
|
public static string XmlSerialize<T>(T obj)
|
|
{
|
|
string s;
|
|
|
|
//派生类序列化
|
|
XmlSerializer xml = new XmlSerializer(obj.GetType(), Types);
|
|
using (MemoryStream ms = new MemoryStream())
|
|
{
|
|
xml.Serialize(ms, obj);
|
|
s = Encoding.UTF8.GetString(ms.ToArray());
|
|
}
|
|
return s;
|
|
}
|
|
}
|
|
}
|