不包括序列化过程中的一些属性不改变原班 [英] Excluding some properties during serialization without changing the original class

查看:126
本文介绍了不包括序列化过程中的一些属性不改变原班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图序列化一个对象有几个属性,但是我并不想在序列化的所有属性。另外,我想更改日期格式。

I'm trying to serialize an object with several properties, but I don't want to include all properties in the serialization. Also, I would like to change the date format.

当然,我可以添加 [XmlIgnore] ,但我不能改变原有的类。

Of course I could add [XmlIgnore], but I'm not allowed to change the original class.

我能想到的唯一的选择是创建一个新的类,并复制这两个类之间的所有内容。但是,这将是丑陋的,需要大量的人工code的。

The only option I could think of was to create a new class and copy all contents between the two classes. But that would be ugly and would require a lot of manual code.

难道也许是可能的,也许创建一个子类,因为原来不是抽象的?

Would it maybe be possible to maybe create a subclass, since the original is not abstract?

我的问题是这样的:


  1. 我怎么能排除一些属性不改变原班?

  1. How can I exclude some properties without changing the original class?

我如何自定义输出XML的日期格式?

How can I customize the date format of the output XML?

要求:


  1. 由于强类型尽可能

  1. As strong typed as possible

XML序列化应该是deserializable

Serialized XML should be deserializable

先谢谢了。

推荐答案

有关任何有兴趣,我决定用 XmlAttributeOverrides ,而且使他们更加强类型(我恨键入属性名称作为字符串)。下面是我用它的扩展方法:

For whoever is interested, I decided to use XmlAttributeOverrides, but made them more strong typed (I hate to type property names as strings). Here is the extension method I used for it:

    public static void Add<T>(this XmlAttributeOverrides overrides, Expression<Func<T, dynamic>> propertySelector, XmlAttributes attributes)
    {
        overrides.Add(typeof(T), propertySelector.BuildString(), attributes);
    }

    public static string BuildString(this Expression propertySelector)
    {
        switch (propertySelector.NodeType)
        {
            case ExpressionType.Lambda:
                LambdaExpression lambdaExpression = (LambdaExpression)propertySelector;
                return BuildString(lambdaExpression.Body);

            case ExpressionType.Convert:
            case ExpressionType.Quote:
                UnaryExpression unaryExpression = (UnaryExpression)propertySelector;
                return BuildString(unaryExpression.Operand);

            case ExpressionType.MemberAccess:

                MemberExpression memberExpression = (MemberExpression)propertySelector;
                MemberInfo propertyInfo = memberExpression.Member;

                if (memberExpression.Expression is ParameterExpression)
                {
                    return propertyInfo.Name;
                }
                else
                {
                    // we've got a nested property (e.g. MyType.SomeProperty.SomeNestedProperty)
                    return BuildString(memberExpression.Expression) + "." + propertyInfo.Name;
                }

            default:
                // drop out and throw
                break;
        }
        throw new InvalidOperationException("Expression must be a member expression: " + propertySelector.ToString());
    }

然后,忽略属性,我可以漂亮将其添加到忽略列表:

Then, to ignore an attribute, I can beautifully add it to the ignore list:

    var overrides = new XmlAttributeOverrides();
    var ignore = new XmlAttributes { XmlIgnore = true };
    overrides.Add<MyClass>(m => m.Id, ignore);
    overrides.Add<MyClass>(m => m.DateChanged, ignore);
    Type t = typeof(List<MyClass>);
    XmlSerializer serial = new XmlSerializer(t, overrides);

这篇关于不包括序列化过程中的一些属性不改变原班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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