序列化不适用于多个项目
我正在使用 xna 框架为 Windows Phone 创建游戏。 我想将我的关卡存储在 xml 文件中,所以我想我将使用 xml 序列化和反序列化。我首先创建了一个测试项目,看看这是否可行,并且没有任何问题。
现在我已经创建了一个关卡编辑器,它是一个 Windows 窗体应用程序,我用它来构建关卡,而不是我想要序列化它们并在不同的项目(Windows Phone)中反序列化它。为了保持对象相同,我在 Windows 窗体应用程序中引用了 Windows Phone 应用程序。我在序列化中使用了相同的参考文献。
使用 System.Xml.Serialization;
但是当我尝试序列化对象时,我得到一个异常: 反映类型时出错。
所以我的问题是为什么我不能序列化来自不同项目的对象? 或者这是不可能的?
public static void SaveLevel(Level level)
{
FileStream fs = new FileStream(@"c:\level1.xml", FileMode.Create);
MemoryStream ms = new MemoryStream();
Serialize(ms, level);
fs.Write(ms.ToArray(), 0, (int)ms.Length);
ms.Close();
fs.Close();
ms.Dispose();
fs.Dispose();
}
public static void Serialize(Stream streamObject, object objectForSerialization)
{
if (objectForSerialization == null || streamObject == null)
return;
XmlSerializer serializer = new XmlSerializer(objectForSerialization.GetType()); //error occurs
serializer.Serialize(streamObject, objectForSerialization);
}
需要序列化的对象。
public class Level
{
[XmlElement]
public string Name { get; set; }
public Level()
{
}
}
一旦我将文件复制到 levelEditor 项目,它将对其进行序列化和反序列化。 但我实际上不想复制这些文件,因为如果我更改一个项目中的文件,我还需要在另一个项目中更改它,这不是很方便。
I'm creating a game for the windows phone with the xna framework.
I want to store my levels in an xml file so i thought i'll be using xmlserialization and deserialization. I've first created a test project to see if this would work and it does no problems there.
Now I've created a level editor and it's an windows forms application which i use to build the levels than i want to serialize them and in a different project (Windows Phone) deserialize it. In order to keep the objects the same i've referenced the windows phone application in the windows forms application. I've used the same references for the serialization.
using System.Xml.Serialization;
But when i try to serialize the object i get an exception:
There was an error reflecting type .
So my question is why can't i serialize objects from a different project?
Or is this just not possible?
public static void SaveLevel(Level level)
{
FileStream fs = new FileStream(@"c:\level1.xml", FileMode.Create);
MemoryStream ms = new MemoryStream();
Serialize(ms, level);
fs.Write(ms.ToArray(), 0, (int)ms.Length);
ms.Close();
fs.Close();
ms.Dispose();
fs.Dispose();
}
public static void Serialize(Stream streamObject, object objectForSerialization)
{
if (objectForSerialization == null || streamObject == null)
return;
XmlSerializer serializer = new XmlSerializer(objectForSerialization.GetType()); //error occurs
serializer.Serialize(streamObject, objectForSerialization);
}
Object that needs to be serialized.
public class Level
{
[XmlElement]
public string Name { get; set; }
public Level()
{
}
}
Once i've copied the file to the levelEditor project it will serialize and deserialize it.
But i actually don't want to copy those files because if i change a file in one project i also need to change it in the other project which isn't very handy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,要序列化自定义类型,您必须为该类提供 [Serializable] 属性。像这样。
看看是否能解决问题。
As far as i knew, to serialize a custom type, you have to give the class a [Serializable] attribute. like this.
see if that solves it.