0

I'd like to have an enum attribute to be optional.
I guess the way to achieve that would be for it to be nullable.

[XmlAttribute(AttributeName = "action")]
public EAction? Action
{
    get;
    set;
}

But this won't work.

With my current knowledge on the matter the only valid nullable tpye I could use is string.
But if it's possible I'd rather not use the trick where you just hide the Enum with a string.

[XmlIgnore]
public EAction? Action
{
    get;
    set;
}

[XmlAttribute(AttributeName = "action")]
public string _Action
{
    get { return this.Action.ToString();
    set { ... }
}

Am I asking for the impossible?

1 Answer 1

0

I'm not trying to have nullable attributes now.

Instead I'm using the ShouldSerialize pattern (which I recently found out about).
So after adding a "None" value to my enum, it goes like this:

    [XmlAttribute(AttributeName = "action")]
    public EAction Action
    {
        get;
        set;
    }

    public bool ShouldSerializeAction()
    {
        return this.Action != EAction.None;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.