'Microsoft.DirectX.Direct3DX is not marked as serializable.

 am trying to store mesh data and texture in a serializable bin file

[Serializable]
public class EvoD3DSceneObject {

public Microsoft.DirectX.Direct3D.Mesh mesh = null;
public Microsoft.DirectX.Direct3D.Material[] meshMaterials;
public Microsoft.DirectX.Direct3D.Texture[] meshTextures;
public Microsoft.DirectX.Direct3D.ExtendedMaterial[] materials = null;

}

error:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Runtime.Serialization.SerializationException: Type 'Microsoft.DirectX.Direct3D.Mesh' in Assembly 'Microsoft.DirectX.Direct3DX, Version=1.0.2904.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.

  at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
  at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
  at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
  at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
  at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
  at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
  at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
  at EvoD3DLIB.EvoD3DSceneCollection.Save()
  at UroborosChat.FormMain.FormMain_Load(Object sender, EventArgs e)
  at System.Windows.Forms.Form.OnLoad(EventArgs e)
  at System.Windows.Forms.Form.OnCreateControl()
  at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
  at System.Windows.Forms.Control.CreateControl()
  at System.Windows.Forms.Control.WmShowWindow(Message& m)
  at System.Windows.Forms.Control.WndProc(Message& m)
  at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
  at System.Windows.Forms.ContainerControl.WndProc(Message& m)
  at System.Windows.Forms.Form.WmShowWindow(Message& m)
  at System.Windows.Forms.Form.WndProc(Message& m)
  at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


can any one help plase



Answer this question

'Microsoft.DirectX.Direct3DX is not marked as serializable.

  • Jac Tremblay

    That's probably a question for the DirectX Developers Mailing List at the moment. You can subscribe through this interface:

    http://discuss.microsoft.com/archives/directxdev.html

    It would certainly be nice to see the DirectX developer community transition to these forums (hint, hint).

    Anyway, I'll take a crack at your question.

    You're right, the Microsoft.DirectX.Direct3D.Mesh class is not marked as Serializable. Mesh data is stored in the XFile format, and so it doesn't really coexist very well with the .NET serialization mechanism. You'll note that you have the Mesh object as the first member of your class. Since Mesh isn't serializable by the CLR, an exception is thrown and execution is halted. But the truth is, the other managed Direct3D classes you have there also are not serializable, and you just haven't experienced it yet.

    So what is the solution Managed Direct3D exposes custom serialization methods for each of those classes. For example, Mesh.FromFile and Mesh.Save. Take a look at the Managed DirectX documentation for a full explanation of these methods.

    So, the bottom line is, you can't serialize objects of these types using .NET serialization. So what do you do I would just serialize out the filenames of the X Files that represent the scene. To do this, I would implement ISerializable and provide custom serialization for the EvoD3DSceneObject class. And EvoD3DSceneObject.GetObjectData, instead of trying to write out the actual Direct3D objects, I'd just write out the file names of the external files that the data will be stored in. Then I would write out each Direct3D object using its own custom Save methods to the external files.

    Perhaps some code would better illustrate this:


    using Microsoft.DirectX;using Microsoft.DirectX.Direct3D;using System;using System.Runtime.Serialization;using System.Security.Permissions;[Serializable]public class EvoD3DSceneObject : ISerializable{    private String name = String.Empty;    private Mesh mesh;    private ExtendedMaterial[] meshMaterials;    private Texture[] meshTextures;    public String Name    {        get { return name; }        set { name = (value != null)   value : String.Empty; }    }    [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]    protected EvoD3DSceneObject(SerializationInfo info, StreamingContext context)    {        Device device = (Device)context.Context;        String meshFileName = info.GetString("MeshFileName");        name = meshFileName.Substring(0, meshFileName.LastIndexOf('.'));        mesh = Mesh.FromFile(meshFileName, MeshFlags.Managed, device, out meshMaterials);        for (Int32 index = 0; index < meshTextures.Length; ++index)        {            meshTextures[index] = TextureLoader.FromFile(device, meshMaterials.TextureFilename);        }    }    [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]    public void GetObjectData(SerializationInfo info, StreamingContext context)    {        String meshFileName = this.Name + ".x";        info.AddValue("MeshFileName", meshFileName);        Int32[] adjacencyList = new Int32[3 * mesh.NumberFaces];        mesh.GenerateAdjacency(0.0001f, adjacencyList);        mesh.Save(meshFileName, adjacencyList, materials, XFileFormat.Compressed);        for (Int32 index = 0; index < meshTextures.Length; ++index)        {            TextureLoader.Save(meshMaterials[index].TextureFilename, ImageFileFormat.Png, meshTextures[index]);        }    }}
     


    I make no guarantee that the above code will work or compile as is, there's still a lot of issues that need to be resolved. And I'll leave figuring out how to pass in custom context objects (such as the Direct3D device for deserialization) to the object serializer as an excersize for you--doing this should let you have the caller custom taylor the serialization and file formats to use and what not.

    - Jesse Towner

  • Shabdar

    tanks Jesse Towner

    i think i vill dump the mesh into a vertexbuffer
    becourse namespace:
    Microsoft.directx; is serializable it works width net 2.0

    all my matrix opratings works fine

    _____so directx has a bug__________.

    hope that microsoft will make that forum 
    becourse directx has a lag in info
    so i think this will be a good place to start

    i vill allso be happy to write some articles to the forum
    in my current 3d engine at www.3devolution.net
    it renders 20000 2d sprites 30 fps
    and 15000 3d sprite 30 fps on a geforce fx 5200
    witch is a very bad graphic card to do directx programming on

    best regards 
    michael hansen


     




  • dotnetprogram

    Hi Michael -
    I noticed that you've already posted your suggestion to the Ideas and Suggestions page.  Smile

    For now, you can find the DirectX Support and Community page here:
    http://msdn.microsoft.com/directx/directxcommunity/default.aspx
    as well as all the current Directx usenet newsgroups.

    Thanks!
    Karen Liu
    Visual C#

  • 'Microsoft.DirectX.Direct3DX is not marked as serializable.