C#:接口中的枚举 [英] C#: Enums in Interfaces

查看:374
本文介绍了C#:接口中的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这个问题的几个相似的线索,但是他们都没有真正回答我想问的问题。

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

对于初学者,不幸的是我使用现有的API代码,可悲的是,虽然可能有更好的方式来做我所问的事情,但是我被锁定做与之相似的方式,因为向后兼容性是不可协商的。

For starters, unfortunately I'm working with existing API code so sadly, while there may be a better way to do what I'm asking about, I'm locked in to doing it similarly to the way it is because backwards compatibility is non-negotiable.

我有一个响应类,当前包含一个错误代码和字符串描述的枚举。错误代码定义了一个非常好的和完整的响应集,它们都非常语义上与其使用的操作相结合。

I have a response class that currently contains an enum for an error code and a string description. The error codes define a fairly nice and complete set of responses that are all very semantically coupled to the operations where they're used.

不幸的是,我现在必须添加一个对于类似的一组API对象,不同的工作流程,这将需要一个字符串描述,这是很好的,但也是一个由完全不相关的错误代码组成的枚举错误代码。错误代码(和对象模型的其他方面)将被使用在很多相同的类中,所以获得一个界面是很好的,以便我可以通过相同的框架运行对象。

Unfortuantely, I now have to add a different workflow for a similar set of API objects, and this will require a string description, which is fine, but also an enum error code consisting of a totally unrelated set of error codes. The error codes (and other aspects of the object model) will be used in lots of the same classes, so it would be nice to get a interface going so that i can run the objects through the same framework.

这里的意图是制定一个我有错误代码和该错误代码的描述的合同。

The intent here is to make a contract that says "I have an error code, and a description of that error code."

然而,据我所知,没有办法在界面上添加一个项目,例如

However, as far as I know there's no way to add an item to an interface such as

public interface IError
{
    enum ErrorCode;
    string Description;
}

也没有办法表达

public interface IError<T> where T: enum
{
    T ErrorCode;
    string Description;
}

任何人都会遇到这样的事情?

Anyone every run up against something like this before?

推荐答案

是的,我已经反对这个了。不是在这种特殊情况下,而是在其他Stack Overflow问题中,像这样一个。 (我没有投票关闭这个作为重复,因为它有点不同。)

Yes, I've run up against this. Not in this particular situation, but in other Stack Overflow questions, like this one. (I'm not voting to close this one as a duplicate, as it's slightly different.)

可以表达你的通用界面 - 只是不在C#。你可以在IL中做到没有任何问题。我希望在C#5中可以删除限制。C#编译器实际上正好处理了约束,就我所见。

It is possible to express your generic interface - just not in C#. You can do it in IL with no problems. I'm hoping the limitation may be removed in C# 5. The C# compiler actually handles the constraint perfectly correctly, as far as I've seen.

如果你真的想作为选项,您可以使用与无约束旋律,我有一个图书馆,暴露了各种方法与这个难以产生的约束。它使用IL重写,有效 - 这是粗糙的,但它适用于UM,也可能为您工作。你可能希望将界面放在一个单独的程序集中,这会有些尴尬。

If you really want to go for this as an option, you could use code similar to that in Unconstrained Melody, a library I've got which exposes various methods with this hard-to-produce constraint. It uses IL rewriting, effectively - it's crude, but it works for UM and would probably work for you too. You'd probably want to put the interface into a separate assembly though, which would be somewhat awkward.

当然,你可以让你的界面只有 T:struct 而不是理想,但至少会限制类型。只要你可以确保它不被滥用,那么这个工作就会很好。

Of course, you could make your interface just have T : struct instead... it wouldn't be ideal, but it would at least constrain the type somewhat. So long as you could make sure it wasn't being abused, that would work reasonably well.

这篇关于C#:接口中的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆