Object Serialization and IDisposable
public static string SerializeObject<T>(T obj)
{
try
{
string xmlString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(T));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, obj);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
xmlString = UTF8ByteArrayToString(memoryStream.ToArray()); return xmlString;
}
catch
{
return string.Empty;
}
}
this is from - http://geekswithblogs.net/paulwhitblog/archive/2007/07/20/114076.aspx
Is there a way to write this where the memoryStream object doesn't need开发者_如何学JAVA to be reassigned? Shouldn't it be disposed (wrapped in a using block)? What is the memoryStream used then reassigned?
I am not understanding the memory stream reassignment either. Here is my refactored version.
public static string SerializeObject<T>(T obj)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
XmlSerializer xs = new XmlSerializer(typeof(T));
XmlTextWriter xwriter = new XmlTextWriter(ms, Encoding.UTF8);
xs.Serialize(xwriter, obj);
return UTF8ByteArrayToString(ms.ToArray());
}
}
catch
{
return string.Empty;
}
}
You are correct. You should wrapped the stream in a using block. And the reassignment is not allowed when the variable is used in the using block. And its not even needed to do the serializing. You could do it like this:
public static string SerializeObject<T>(T obj)
{
try
{
string xmlString = null;
using (MemoryStream memoryStream = new MemoryStream())
{
XmlSerializer xs = new XmlSerializer(typeof(T));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, obj);
xmlString = UTF8ByteArrayToString(memoryStream.ToArray());
return xmlString;
}
}
catch
{
return string.Empty;
}
}
I would refactor this code slightly differently to other answers to first check if the type is serializable. There's is no way to do this by using constraints (http://bit.ly/c2Hq4s) but you can check it easily enough anyway. I just left out the using and included the Finally just to show that you could do it without the using also.
public static string SerializeObject<T>(T obj)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("type is not serializable");
}
string xmlString = string.Empty;
MemoryStream memoryStream = new MemoryStream();
try
{
XmlSerializer xs = new XmlSerializer(typeof(T));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, obj);
xmlString = UTF8ByteArrayToString(memoryStream.ToArray());
}
catch // something useful here
{
// Do something useful here
}
finally
{
// Dispose of what you want here
}
return xmlString;
}
In case you want to use the current encoding, here is another example of the same thing.
public static string SerializeObject<T>(T o)
{
string serializeObject = string.Empty;
if (o != null)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
XmlSerializer xs = new XmlSerializer(typeof(T));
xs.Serialize(ms, o);
using (System.IO.StreamReader sr = new StreamReader(ms))
{
serializeObject = sr.CurrentEncoding.GetString(ms.ToArray());
}
}
}
catch { }
}
return serializeObject;
}
public static string SerializeObject<T>(T o)
{
var serialized = "";
try
{
using (var ms = new MemoryStream())
{
var xs = new XmlSerializer(typeof(T));
xs.Serialize(ms, o);
using (var reader = new StreamReader(ms))
{
serialized = sr.CurrentEncoding.GetString(ms.ToArray());
}
}
}
catch
{
// bad stuff happened.
}
return serialized;
}
精彩评论