简体   繁体   English

具有可选参数的构造函数上的SerializableAttribute

[英]SerializableAttribute on a constructor with optional parameters

Is there a technical reason that the serialization of an object is not working, when the constructor has parameters, but each of the parameters has a default value? 当构造函数有参数但每个参数都有默认值时,是否存在技术原因导致对象的序列化不起作用?

For example, lets say this is the a (pseudo-)class I would like to serialize: 例如,假设这是我想要序列化的(伪)类:

[SerializableAttribute]
public class Parameter
{
    public Parameter(int p1 = -1, int p2 = -1, int p3 = -1, AnotherEnum p4 = AnotherEnum.Enum1)
    {
        P1 = p1;
        P2 = p2;
        P3 = p3;
        m_enum = p4;
    }

    private AnotherEnum m_enum;

    [DataMember(Name = "P1")]
    public int P1 { get; set; }


    [DataMember(Name = "P2")]
    public int P2 { get; set; }


    [DataMember(Name = "P3")]
    public int P3 { get; set; }

    [DataMember(Name = "P4")]
    public AnotherEnum Enum
    {
        get
        {
            return m_enum;
        }
        set
        {
            m_enum = value;
        }
    }
}

This would give me an exception: 这会给我一个例外:

Namespace.Parameter cannot be serialized because it does not have a parameterless constructor. Namespace.Parameter无法序列化,因为它没有无参数构造函数。

One workaround would be like this: 一种解决方法是这样的:

public Parameter() // or this(-1)
{
    P1 = -1;
    P2 = -1;
    P3 = -1;
    m_enum = AnotherEnum.Enum1;
}

public Parameter(int p1 /* remove default value */, int p2 = -1, int p3 = -1, AnotherEnum p4 = AnotherEnum.Enum1)
{
    P1 = p1;
    P2 = p2;
    P3 = p3;
    m_enum = p4;
}

I would like to know, why exactly the XMLSerializer does not see a constructor with parameters that all have default values. 我想知道,为什么XMLSerializer没有看到带有参数的构造函数都具有默认值。 And are there better work arounds for this? 那有更好的解决方法吗?

Thanks in advance! 提前致谢!

We have to go to the compiler level to understand that the method with optional parameters are actually compile-time substitution of the method with non-optional parameters. 我们必须转到编译器级别才能理解带有可选参数的方法实际上是使用非可选参数对方法进行编译时替换

In other words, there is no real "optional parameter with default value" . 换句话说, 没有真正的“带有默认值的可选参数” It is a "non-optional-parameter substituted at compile time" but coated with syntatical sugar called "optional parameter with default parameter" since C#4.0 during the design time . 它是一个“在编译时替换的非可选参数”,在设计时使用了 C#4.0以来称为“带有默认参数的可选参数”的合成糖

"optional parameter with default value" is only a concept at the design time, not at the compile/run time. “具有默认值的可选参数”仅是设计时的概念,而不是编译/运行时的概念。

To illustrate, assuming you have: 为了说明,假设你有:

public Parameter(int p1 = -1, int p2 = -1, int p3 = -1, AnotherEnum p4 = AnotherEnum.Enum1)
{
    P1 = p1;
    P2 = p2;
    P3 = p3;
    m_enum = p4;
}

And you do: 你也是:

Parameter pa = new Parameter(1);
Parameter pb = new Parameter(1, -1);

The two of them will be resulting in identical Intermediate Language (IL), although in design time they look different(!). 它们中的两个将产生相同的中间语言(IL),尽管在设计时它们看起来不同(!)。

Additional note: as for why the XMLSerializer requires parameterless constructor. 附加说明:至于为什么XMLSerializer需要无参数构造函数。 It is well-explained here . 这里有很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM