How to set acceptable single values for an Enum property in a Serializable class?

Asked

Viewed 66 times

1

Given a enum as an example:

public enum IcmsCst
{
    [XmlEnum("00")] Cst00,
    [XmlEnum("10")] Cst10,
    [XmlEnum("20")] Cst20,
    ...
}

There is a form of annotation so that I could specify that a certain type of ICMS could accept only a certain type of CST that Enum?

For example:

public class Imposto
{
    [XmlEnum(AcceptOnly = [IcmsCst.Cst00])] // como exemplo do que busco para a limitação
    public ICMS Icms00 { get; set; }
}

And with that have the validation done automatically when serialize the object Imposto in an XML?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

No specific mechanism ready. It is possible to create a framework complete to treat it. Or do the simple and time to import the information treat whether it is valid or not. Generic mechanisms are useful to be reused. So there are when it’s something everyone needs, or you should do yours, if you need to use it often and it brings advantages.

Even if you do, you need to have a very well thought-out way of how to treat it, which may not be a universal way. You don’t accept it, so what? Even this point is not defined in the question, without the clear criterion there is no good solution. In any case, it is very difficult to establish these criteria in code in a declarative form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.