[英]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
? 有什么好的方法来创建
MandatoryOrOptionalParameters
类型来避免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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.