从'System.IO.Stream'到'System.IO.MemoryStream'的隐式向下转换

时间:2012-11-13 07:49:04

标签: .net casting unity3d downcast unityscript

function SerializeObject(pObject : Object)
{
   var XmlizedString : String  = null;
   var memoryStream : MemoryStream  = new MemoryStream();
   var xs : XmlSerializer = new XmlSerializer(typeof(XmlData));
   var xmlTextWriter : XmlTextWriter  = new XmlTextWriter(memoryStream, Encoding.UTF8);
   xs.Serialize(xmlTextWriter, pObject);
   memoryStream = xmlTextWriter.BaseStream;
   XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
   return XmlizedString;
}

问题在于:

memoryStream = xmlTextWriter.BaseStream;

我知道xmlTextWriter.BaseStream属于类型流,但是如何进行类型转换呢?

感谢先进的任何帮助!

1 个答案:

答案 0 :(得分:1)

您可以将as用于参考类型。

尝试:

memoryStream = xmlTextWriter.BaseStream as MemoryStream;

有关详细信息,请参阅此问题:How to perform Explicit typecasting in UnityScript?