![](/img/trans.png)
[英]What is the easiest way to save an Observable collectin of object to an XML file?
[英]What's the easiest way to save an object to a file without serialization attributes?
我在c#中有不同類型的對象,我想保存到文件(XML是首選)但我不能使用序列化,因為該類不是由我編寫的,而是來自DLL。
什么是最好的解決方案?
我最終使用了JavaScriptSerializer,它完全符合我的要求:
List<Person> persons = new List<Person>();
persons.Add(new Person(){Name = "aaa"});
persons.Add(new Person() { Name = "bbb" });
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
var strData = javaScriptSerializer.Serialize(persons);
var persons2 = javaScriptSerializer.Deserialize<List<Person>>(strData);
鑒於一個不可序列化的對象,我已經提出了一個快速的小擴展方法,它將“序列化”為XML。 它非常粗糙,並且不會進行大量檢查,並且它生成的XML可以輕松調整以滿足您的需求:
public static string SerializeObject<T>(this T source, bool serializeNonPublic = false)
{
if (source == null)
{
return null;
}
var bindingFlags = BindingFlags.Instance | BindingFlags.Public;
if (serializeNonPublic)
{
bindingFlags |= BindingFlags.NonPublic;
}
var properties = typeof(T).GetProperties(bindingFlags).Where(property => property.CanRead).ToList();
var sb = new StringBuilder();
using (var writer = XmlWriter.Create(sb))
{
writer.WriteStartElement(typeof(T).Name);
if (properties.Any())
{
foreach (var property in properties)
{
var value = property.GetValue(source, null);
writer.WriteStartElement(property.Name);
writer.WriteAttributeString("Type", property.PropertyType.Name);
writer.WriteAttributeString("Value", value.ToString());
writer.WriteEndElement();
}
}
else if (typeof(T).IsValueType)
{
writer.WriteValue(source.ToString());
}
writer.WriteEndElement();
}
return sb.ToString();
}
我在這堂課上測試了它:
private sealed class Test
{
private readonly string name;
private readonly int age;
public Test(string name, int age)
{
this.name = name;
this.age = age;
}
public string Name
{
get
{
return this.name;
}
}
public int Age
{
get
{
return this.age;
}
}
}
以及數字3
和object
。 生成的XML如下:
<?xml version="1.0" encoding="utf-16"?>
<Test>
<Name Type="String" Value="John Doe" />
<Age Type="Int32" Value="35" />
</Test>
<?xml version="1.0" encoding="utf-16"?>
<Int32>3</Int32>
<?xml version="1.0" encoding="utf-16"?>
<Object />
分別。
在DLL的非可序列化類周圍編寫自己的可序列化包裝器。
編輯:在評論中建議使用AutoMapper,我還沒有聽說過,但現在我已經肯定會使用它而不是自己編寫包裝器。 除非需要一些反射來捕獲非序列化對象的一些內部狀態(如果可能),我不知道AutoMapper是否有任何東西可以提供,或者你必須看看你是否可以在你的包裝器中捕獲它。
我會編寫一個POCO(Plain Old Class Object)類,它模仿返回的DLL中的對象。 通常,如果您使用,我相信,.NET 3.5或更高版本,您可以使用LINQ。 我贊成Linq將對象放入其他類或對它們執行排序或其他操作。
這是一個簡單的例子,例如你可以在你的返回對象中使用dummying。 請記住,在DLL中,您可以擁有許多不同的對象並多次執行此操作。 我也會將我的方法包裝在他們自己的類中以獲得可用性而不是在主要方面。 但這是一個簡單的概念證明
using System;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Xml.Linq;
namespace ExampleSerializer
{
class Program
{
// example class to serialize
[Serializable]
public class SQLBit
{
[XmlElement("Name")]
public string Name { get; set; }
[XmlText]
public string data { get; set; }
}
// example class to populate to get test data
public class example
{
public string Name { get; set; }
public string data { get; set; }
}
static void Main(string[] args)
{
string s = "";
// make a generic and put some data in it from the test
var ls = new List<example> { new example { Name = "thing", data = "data" }, new example { Name = "thing2", data = "data2" } };
// make a second generic and put data from the first one in using a lambda
// statement creation method. If your object returned from DLL is a of a
// type that implements IEnumerable it should be able to be used.
var otherlist = ls.Select(n => new SQLBit
{
Name = n.Name,
data = n.data
});
// start a new xml serialization with a type.
XmlSerializer xmler = new XmlSerializer(typeof(List<SQLBit>));
// I use a textwriter to start a new instance of a stream writer
TextWriter twrtr = new StreamWriter(@"C:\Test\Filename.xml");
// Serialize the stream to the location with the list
xmler.Serialize(twrtr, otherlist);
// Close
twrtr.Close();
// TODO: You may want to put this in a try catch wrapper and make up your
// own classes. This is a simple example.
}
}
}
我認為問題標題中的“無序列化”一詞具有誤導性。
如果我理解正確你想要序列化沒有serilisation屬性的對象。
像sharpserializer和protobuf-net這樣的庫可以為你完成這項工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.