c#:如何隐藏仅用于 XML 序列化逆向兼容性的字段? [英] c#: how to hide a field which is used only for XML serialization retrocompatibility?

查看:46
本文介绍了c#:如何隐藏仅用于 XML 序列化逆向兼容性的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该字段仅在序列化/反序列化过程中使用,但我想立即封装它并从类中隐藏.

The field is used only during the serialization / deserialization process but I would like to immediately encapsulate it and hide from the class.

有可能吗?

推荐答案

在 C# 中使用 XML 序列化是不可能的,如果你想这样做,你应该使用 DataContractSerialization,它允许这种功能,即您可以序列化对象的私有字段.

Its not possible with XML-Serialization in C# , if you want to do like that than you should make use of DataContractSerialization, It allows this kind of functionality i.e. you can serialize private field of you object.

下面可以使用DataContractSerialization,希望你喜欢尝试

Below is possible with DataContractSerialization, I hope you like to try out

[DataContract]
class Person
{
    [DataMember]
    public string m_name;

    [DataMember]
    private int m_age;
}

<小时>

这是我在学习 XML to Linq 时尝试过的,这是有线解决方案,但如果您想尝试,这里我通过使用 xml to linq 创建了 xml 字符串


This what I tried when I was learning XML to Linq , and this is wired solution but if you want to try , here i created xml string by using xml to linq

这是我的文章:反对使用 LINQ 或 XmlSerializer 的 XML

注意:这里产品类的代码字段是私有字段,但您仍然可以生成xml字符串

Note : here code field of product class is private field but still you can generate xml string

using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;

class Program
{
     public class Product
        {
            public Product()
            { }

            public Product(string name,int code, List<productType> types)
            {
                this.Name = name;
                this.Code = code;
                this.types = types;
            }

            public string Name { get; set; }
            private int Code { get; set; }
            public List<productType> types { get; set; }

            public string Serialize(List<Product> products)
            {
                XElement productSer = new XElement("Products",
               from c in products
               orderby c.Code
               select new XElement("product",
                   new XElement("Code", c.Code),
                   new XElement("Name", c.Name),
                   new XElement("Types", (from x in c.types
                                          orderby x.type//descending 
                                           select new XElement("Type", x.type))
               ))
          );
                return productSer.ToString();
            }
        }
        public class productType
        {
            public string type { get; set; }
        }

 public static void Main()
    {    
        List<productType> typ = new List<productType>();
        typ.Add((new productType() { type = "Type1" }));
        typ.Add((new productType() { type = "Type2" }));
        typ.Add((new productType() { type = "Type3" }));

        List<Product> products =new List<Product>() { new Product ( "apple", 9,typ) ,
                      new Product ("orange", 4,typ   ),
                      new Product ("apple", 9 ,typ),
                      new Product ("lemon", 9,typ ) };

   Console.WriteLine(new Product().Serialize(products));
        Console.ReadLine();
 }
}

这篇关于c#:如何隐藏仅用于 XML 序列化逆向兼容性的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆