简体   繁体   中英

C# enum type shared multiple classes

I am trying to find what is the most elegant way to handle the below situation:

class ObjectA
{
    public enum MandatoryParameters {A, B, C}
    public enum OptionalParameters {D, E, F}
}
class ObjectB
{
    public enum MandatoryParameters {G, H, I}
    public enum OptionalParameters {J, K}
}
class Error
{
    MandatoryOrOptionalParameters param;
    string errorDescription;

    Error(MandatoryOrOptionalParameters p, string d)
    {
        param = p;
        errorDescription = d;
    }
}

Is there any good way to create a MandatoryOrOptionalParameters type to avoid string ? I read the few related message here and here .

Create an enum representing any kind of parameters and then each class will get or specify a collection of items representing the optional or the mandatory:

public enum Parameters { A, B, C, D, E }

// Class specifies for itself the mandatory and optional
public class Foo
{
    public IEnumerable<Parameters> Mandatory { get; set; } = new List<Parameters> { A, B };

    public IEnumerable<Parameters> Optional { get; set; } = new List<Parameters> { C, D };
}

// Class receives what are the mandatory and optional
public class Bar
{
    public Bar(IEnumerable<Parameter> optional, IEnumerable<Parameter> mandatory)
    {
        // store input
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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