C# [NonSerializable] and Formatters namespace cannot be found [SOLVED]

Started by
1 comment, last by deadlydog 14 years, 1 month ago
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]
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
Advertisement
NonSerializedAttribute lives in System according to Google/MSDN. And indeed, the most basic app builds fine for me with the default library references:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace test{     [Serializable]    public class foo {        [NonSerialized]        private string bar;    }    class Program {        static void Main(string[] args) {            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();            System.IO.FileStream target = new System.IO.FileStream("test.out",System.IO.FileMode.Create);            formatter.Serialize(target, new foo());        }    }}


The only thing I can think of is you're trying to use a non-standard (perhaps for xbox or handhelds?) .NET framework that doesn't include the serialization stuff.
Hmmmm, yes, I tried creating a new test XNA 3.1 Game project, and it compiled and serialized fine. So the problem does seem to be specific to my one project.

....aahhhh, I just figured it out. I didn't notice, but in the compiler warning it says it is for the XBox 360 project.

I have the compiler set to mixed mode, so it builds both the Windows and XBox 360 copy of the project, and it is only the XBox 360 copy the rejects the serializable attribute. I'm not sure why they wouldn't define the NonSerialized attribute and Formatters namespace for the XBox 360 assemblies, as I'm sure they could be used to transfer data to XBox Live and whatnot, but that was my problem.

If I just do a Windows build then everything works perfectly. Thanks.
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA

This topic is closed to new replies.

Advertisement