0

Hello i need to skip a determinate field group in my library. Reason: Cross porting.

One my problem is during deserialization. I Have a Editor and Client.

Editor serialize information, list and graphics component. But the client dont have possibility to deserialize a graphics element,

my code:

 //DirectX Light
 [Serializable]
        public struct _light
        {
            public int id;
            public float Color1;
            public float Color2;
            public float Color3;
            public float Color4;
            public float Power;
            public int decay;
            public float x;
            public float y;
            public float z;
            public bool enabled;
        }


    [Serializable]
            public struct _ev
            {
                public int evntID;
                public int PositionX;
                public int PosotionY;
                public List<string> ComCode;
                public byte[] EventGraphics;
                public List<Graphics.Node> NodeGraph; //Editor only information
                public List<pages> Pages;
                public List<EventItem> Event;

            }

i need to read this field on the Editor and no to Client. But client and Editor use same file for reading information.

Problem are inside to List<Graphics.Node> it's a windows component. and client cannot read this. Giving me back an exception.

This is a my Simple BluePrint Code Generator

Windows Graphics Component

maybe i can skip all field on Client and no to editor. but this struct is a vital for the Editor.

Solution?

Joe Martiniello
  • 323
  • 1
  • 3
  • 13
  • This is not an answer but your structs are horrible. First. you are using public fields. Second, your structs are mutable (and must be, if you want to deserialize them). I would suggest converting them to classes and changing all the public fields to public properties (simply adding `{get;set;}` to each one should be enough). – Zohar Peled Jan 03 '18 at 08:47
  • Separate the data structures used for saving and loading from the ones used internally in your application, or separate the holders of data from the structures holding necessary data for the editor. Basically, don't throw all your eggs in the same basket. Oh, and [mutable structs are evil](https://stackoverflow.com/questions/441309/why-are-mutable-structs-evil), don't use them like this. – Lasse V. Karlsen Jan 03 '18 at 08:48

1 Answers1

3

You can use NonSerialized here is the link to msdn

This is how it should look like as suggested by @ZoharPeled

    [Serializable]
    public class _light
    {
        public int id {get; set;};
        public float Color1 {get; set;};
        public float Color2 {get; set;};
        public float Color3 {get; set;};
        public float Color4 {get; set;};
        public float Power {get; set;};
        public int decay {get; set;};
        public float x {get; set;};
        public float y {get; set;};
        public float z {get; set;};
        public bool enabled {get; set;};
    }


    [Serializable]
    public class _ev
    {
        public int evntID {get; set;};
        public int PositionX {get; set;};
        public int PosotionY {get; set;};
        public List<string> ComCode {get; set;};
        public byte[] EventGraphics  {get; set;};

        //Indicates that a field of a serializable class should not be serialized
        [NonSerialized]
        public List<Graphics.Node> NodeGraph {get; set;}; //Editor only information
        public List<pages> Pages {get; set;};
        public List<EventItem> Event {get; set;};

    }

Or You could make two models, one for client and for your work

    [Serializable]
    public class _ev_backend
    {
        public int evntID {get; set;};
        public int PositionX {get; set;};
        public int PosotionY {get; set;};
        public List<string> ComCode {get; set;};
        public byte[] EventGraphics  {get; set;};
        public List<Graphics.Node> NodeGraph {get; set;}; //Editor only information
        public List<pages> Pages {get; set;};
        public List<EventItem> Event {get; set;};

    }

    [Serializable]
    public class _ev_client
    {
        public int evntID {get; set;};
        public int PositionX {get; set;};
        public int PosotionY {get; set;};
        public List<string> ComCode {get; set;};
        public byte[] EventGraphics  {get; set;};
        public List<pages> Pages {get; set;};
        public List<EventItem> Event {get; set;};

    }
Hey24sheep
  • 1,172
  • 6
  • 16
  • Yes but, List Graphcs.Node serialized file, Editor need to read this field and no for client. I have try to follow this simple help https://msdn.microsoft.com/en-us/library/ms182362.aspx but dont work for me. – Joe Martiniello Jan 03 '18 at 08:58
  • Yes but you can use two files different for editor and client. Or you can use [NonSerialized] if you still want to use one file. I would recommend that you separate the files for editor and client. – Hey24sheep Jan 03 '18 at 09:00
  • "This is how it should look like according to me"... I like the fact you adopted my recommendations in the comments but still claim "according to me". plagiarism is the sincerest form of flattery., [though some will disagree...](https://www.plagiarismtoday.com/2011/05/23/why-plagiarism-is-not-flattery/) – Zohar Peled Jan 03 '18 at 09:04
  • The problem is this. The file serialized is a complete game map information. Contains events, script, graphics ed more element. Especially is optimized for cross platform. I can read whit javascript and wrapped native library for make this map readable for Html5 and WebGL graphics. Your solution is good to make backend struct. i can try this solution tanks @Hey24sheep – Joe Martiniello Jan 03 '18 at 09:07
  • @ZoharPeled I saw your comment after I posted my answer. I have updated it now – Hey24sheep Jan 03 '18 at 09:21
  • @Hey24sheep That's fine, really, I'm amused by it, I'm not angry with you or anything. The things I've pointed out in my comment are common knowledge, I don't claim that's really plagiarism - It's not like I've stumbled upon some hidden truth and you had no way of knowing it beside my comment.... I believe you when you say you didn't even saw my comment before posting your answer. It just find it kinda funny, that's all. – Zohar Peled Jan 03 '18 at 09:30
  • @ZoharPeled It's fine by me. I agree with you that it looks like that I read your comment and posted the answer. :) – Hey24sheep Jan 03 '18 at 09:32