简体   繁体   English

使用静态类型成员以确保非静态类中的类型安全

[英]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中定义的基础值之一的JobStatusnull除外)。 Also, the implicit operator makes the wrapper usable anywhere the JobStatus options are. 同样,隐式运算符使包装程序在JobStatus选项可用的任何地方都可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非静态类中的类型初始化失败 - Type Initialization Failed In Non-Static Class 在非静态类的静态方法中抽象出类型 - Abstract away the type in a static method in a non-static class C#无法访问外部类型的非静态成员 - C# cannot access a non-static member of outer type 无法访问外部类型的非静态成员 - Cannot access a non-static member of outer type 使用反射调用静态类中静态属性的非静态成员 - Call non-static member of static property in static class with reflection 无法使用第三方提供的类通过嵌套类型访问外部类型XXX的非静态成员 - Cannot access a non-static member of outer type XXX via nested type with a third-party provided class 如何使用方法名称获取非静态MethodInfo(不在字符串中,不在类类型中搜索) - How to get non-static MethodInfo using method name (not in string, not searching in class type) 静态或非静态类? - Static or non-static class? 无法通过嵌套类型访问外部类型的非静态成员 - Cannot access a non-static member of outer type via nested type 无法通过嵌套类型“FormMain.ImageDelegateClass”访问外部类型“FormMain”的非静态成员 - Cannot access a non-static member of outer type 'FormMain' via nested type 'FormMain.ImageDelegateClass'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM