C#: Enums in Interfaces

前端 未结 5 638
有刺的猬
有刺的猬 2021-02-07 05:58

I\'ve seen a couple similar threads to this question, but none of them really answer the question I want to ask.

For starters, unfortunately I\'m working with existing A

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-07 07:01

    If I understand what you are wanting to do, then yes, there is no way to define an interface that contains as one of it's members a non-specific enum. Your second example is close, but you are limited to constraining the type of T to a struct, which would allow any value type. At that point, you just have to rely on the knowledge of the interfaces proper usage to let folks know the expected type of T should be an enum. You could potentially make it a little clearer by defining T as TEnum or TErrorValue:

    public interface IError where T: struct
    {
        T ErrorCode;
        string Description;
    }
    

提交回复
热议问题