C#反序列化跳过字段

时间:2018-01-03 08:44:41

标签: c# deserialization

您好我需要跳过库中的确定字段组。 原因:交叉移植。

我的问题是在反序列化期间。 我有一个编辑和客户。

编辑器序列化信息,列表和图形组件。 但是客户端没有可能反序列化图形元素,

我的代码:

 //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;

            }

我需要在编辑器上阅读此字段,而不是客户端。 但是客户端和编辑器使用相同的文件来读取信息。

问题在于List<Graphics.Node>它是一个Windows组件。 而客户端无法阅读此内容。给我一个例外。

这是我的简单BluePrint代码生成器

Windows Graphics Component

也许我可以跳过客户端上的所有字段而不跳过编辑器。 但这个结构对编辑来说至关重要。

解?

1 个答案:

答案 0 :(得分:3)

您可以使用NonSerialized这里是msdn

的链接

这就像@ZoharPeled

所建议的那样
#include

或者您可以制作两个模型,一个用于客户和您的工作

    [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;};

    }