Hi there, I'm having 2 problems trying to get serialization working in C#, both of which are compile time errors:
1 - The first problem I'm having is I'm trying to make my C# class serializable. I have put the [Serializable] attribute in front of my public class declarations, and in front of all the classes/enumerations defined within them. If I do this alone, everything compiles fine. However, I store a handle to an XNA graphics device which I do not want serialized, so I have put the [NonSerialized] attribute in front of it, but when I compile it says "The type or namespace name 'NonSerialized' could not be found (are you missing a using directive or an assembly reference?)" and "The type or namespace name 'NonSerializedAttribute' could not be found (are you missing a using directive or an assembly reference?)". I have tried using [NonSerialized], [NonSerialized()], and [NonSerializedAttribute], but they all give me the same error. Here is a sample code snippet:
using System;
using System.Collections.Generic;
using System.Diagnostics; // Used for Conditional Attributes
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.IO;
using System.Runtime.Serialization;
namespace MyNamespace
{
...
[Serializable]
public class MyClass : MyInterface
{
...
[NonSerialized]
private GraphicsDevice mcGraphicsDevice = null; // Handle to the Graphics Device to draw to
...
}
}
I have also included the System.Runtime.Serialization and System.Runtime.Serialization.Formatters.SOAP .dlls in my project, but it does not seem to make any difference.
Also, I'm not sure if it makes a difference or not, but I'm compiling this project into a .dll, not an executable.
2 - My second problem is that when I try to test out serializing and deserializing, the compiler complains that it cannot find the System.Runtime.Serialization.Formatters namespace, it just gives me the error "The type or namespace name 'Formatters' does not exist in the namespace 'System.Runtime.Serialization' (are you missing an assembly reference?)". I get this error whether I try and include System.Runtime.Serialization.Formatters.SOAP or .Binary in the using statements at the top of the file, or if I declare the full namespace path when declaring my variables. Again, I have included the System.Runtime.Serialization and System.Runtime.Serialization.Formatters.SOAP .dlls in my project but it doesn't seems to make a difference. Here's a sample code snippet of this problem:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using DPSF;
using DPSF.ParticleSystems;
using System.IO;
using System.Runtime.Serialization;
//using System.Runtime.Serialization.Formatters;
//using System.Runtime.Serialization.Formatters.Binary;
//using System.Runtime.Serialization.Formatters.Soap;
namespace TestNamespace
{
public class Game1 : Microsoft.Xna.Framework.Game
{
...
protected override void Initialize()
{
// TODO: Add your initialization logic here
mcParticleSystem = new DefaultPointSpriteParticleSystemTemplate(null);
mcParticleSystem.AutoInitialize(this.GraphicsDevice, this.Content);
base.Initialize();
Stream stream = File.Open("ExplosionFlash.dat", FileMode.Create);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bformatter.Serialize(stream, mcParticleSystem);
stream.Close();
mcParticleSystem = null;
stream = File.Open("ExplosionFlash.dat", FileMode.Open);
bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
mcParticleSystem = (DefaultPointSpriteParticleSystemTemplate)bformatter.Deserialize(stream);
stream.Close();
}
}
}
Any help or suggestions would be greatly appreciated. Thanks in advance.
[Edited by - deadlydog on March 7, 2010 3:32:53 PM]