[英]Using static type member to ensure type-safety in a non-static class
I have a static class defined like this: 我有一个这样定义的静态类:
public static class JobStatus
{
public const string Completed = "Completed";
public const string Failed = "Failed";
public const string Stopped = "Stopped";
}
(this is actually an external library, so can't change this) (这实际上是一个外部库,因此无法更改)
In my non-static class I want a member of that class to ensure that you can only declare it of that "type" 在我的非静态类中,我希望该类的成员确保您只能声明该“类型”
public class JobOutput
{
public string Output { get; set; }
public string OutputError { get; set; }
public JobStatus JobStatus { get; set; }
}
Error: 'JobStatus': static types cannot be used as return types / 'JobStatus': static types cannot be used as parameters 错误:“ JobStatus”:静态类型不能用作返回类型/“ JobStatus”:静态类型不能用作参数
Yeye I know your eyes are bleeding, but I hope you get the point - how can I ensure and achieve a form of type-safety for my JobStatus property? 是的,我知道您的眼睛在流血,但是我希望您明白了-我如何确保和实现JobStatus属性的类型安全形式?
You can't, because all JobStatus
does is contain some members who hold strings . 您不能,因为
JobStatus
所做的只是包含一些持有string的成员。 So you'll have to define your JobStatus
property as a string as well. 因此,您还必须将
JobStatus
属性定义为字符串。
There's no compile-time safety for strings, it could've been an enum instead. 字符串没有编译时的安全性,它本来可以是枚举。
You could add a method SetJobStatus(string status)
to your JobOutput
class and make JobStatus
's setter private. 你可以添加一个方法
SetJobStatus(string status)
到您的JobOutput
类并进行JobStatus
的二传手私人。 Then in that method, you check (using reflection) whether the status
string is present in one of the static class JobStatus
's public const fields. 然后在该方法中,检查(使用反射)
status
字符串是否存在于静态类JobStatus
的公共const字段之一中。 Or you could implement the same in the setter. 或者,您可以在设置器中实现相同的功能。
See How can I get all constants of a type by reflection? 请参见如何通过反射获取类型的所有常量? for information on how to do that.
有关如何执行此操作的信息。 But that's not compile-time safety, but runtime.
但这不是编译时的安全,而是运行时。
You could wrap JobStatus
to make it "type safe" but it looks like a bit of an overkill: 您可以包装
JobStatus
以使其“类型安全”,但看起来有些过头了:
public sealed class JobStatusWrapper
{
public static readonly JobStatusWrapper Completed
= new JobStatusWrapper(JobStatus.Completed);
public static readonly JobStatusWrapper Failed
= new JobStatusWrapper(JobStatus.Failed);
public static readonly JobStatusWrapper Stopped
= new JobStatusWrapper(JobStatus.Stopped);
private readonly string description;
private JobStatusWrapper(string description) {
Debug.Assert(!string.IsNullOrEmpty(description));
this.description = description; }
public static implicit operator string(JobStatusWrapper status)
=> status.description;
}
And now you'd use it: 现在您将使用它:
public class JobOutput
{
//...
public JobStatusWrapper JobStatus { get; set; }
}
And there is no way to pass in or get a JobStatusWrapper
that doesn't have one of the underlying values defined in JobStatus
(except null
). 而且,没有办法传入或获取没有
JobStatusWrapper
中定义的基础值之一的JobStatus
( null
除外)。 Also, the implicit operator makes the wrapper usable anywhere the JobStatus
options are. 同样,隐式运算符使包装程序在
JobStatus
选项可用的任何地方都可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.