开发者

Serializing XNA 4.0

Type 'Microsoft.Xna.Framework.Graphics.RasterizerState' in Assembly 'Microsoft.Xna.Framework.Graphics, Version=4.0.0开发者_JS百科.0, Culture=neutral, PublicKeyToken=842cf8be1de50553' is not marked as serializable.

Im trying to serialize sections of the xna game studio.

Unfortunately i can't directly serialize this class.

Any suggestions ?


Its a common problem. The first question i would ask myself is why your trying to serialize an object which type is not marked as serializable. Did the author of that type leave it off for a reason? Is there a chance that in the future, the type will be expanded with some unserializable behavior?

Then there are 3 solutions in my head. Unfortunatly, you cant directly serialize the object. What you can do is this:

  1. Create a wrapper object (marked as serializable) containing all the properties you want to serialize and copy the values from the actual object to the wrapper object and back.

  2. Extending on option 1: Instread of a simple wrapper object, create a more dynamic wrapper class containing a list of KeyValuePairs where you bind the propertyNames to propertyValues and make some generic code which can fill this list based on a existing object and fill an object based on such a list.

  3. Use unsafe code and do the serialization yourself (pin the object, read its memory and write to a memory stream). << This method is really really unsafe

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜