简体   繁体   English

在c#接口和接口的方法签名之一中实现枚举

[英]Implement enum in c# Interface and in one of interface's method signature

I have an interface 我有一个界面

Interface: 接口:

interface IThing
{
  Enum MyEnum {get;set;}
  string DoAction(MyEnum enumOptionChosen, string valueToPassIn);
}

Concrete implementation: 具体实施:

public class Thing : IThing
{
  public enum MyEnum
  {
    FirstOption,
    SecondOption,
    ThirdOption
  }

  string doAction(MyEnum enumOptionChosen, string valueToPassIn)
  {
    switch(enumOptionChosen)
    {
      case MyEnum.FirstOption:
        x();
        break;
      case MyEnum.SecondOption:
        y();
        break;
      case MyEnum.ThirdOption:
        z();
        break;
    }
  }
}

When I try compiling this I get 'IThing.MyEnum' is a 'property' but is used like a 'type.' 当我尝试编译它时,我得到'IThing.MyEnum'是一个'属性'但是像'类型'一样使用。 I'm missing something about being able to require use of the Enum in the DoAction() signature. 我遗漏了一些能够要求在DoAction()签名中使用Enum的东西。

Thanks for any help. 谢谢你的帮助。

In your interface MyEnum is the name of the variable not the type, so it won't be recognized by the compiler. 在您的界面中, MyEnum是变量的名称而不是类型,因此编译器无法识别它。 You should be able to use Generics to get this done. 您应该能够使用泛型来完成这项工作。

public interface IThing<T>
{
   T MyEnum { get; set; }
   string doAction(T enumOptionChosen, string valueToPassIn);
}

Then you could implement it like this: 然后你可以像这样实现它:

public interface IThing<T>
{
    T MyEnum { get; set; }
    string doAction(T enumOptionChosen, string valueToPassIn);
}

public class Something : IThing<Something.SpecialEnum>
{
    public enum SpecialEnum
    {
        Item1,
        Item2,
        Item3
    }

    public SpecialEnum MyEnum { get; set; }

    public string doAction(SpecialEnum enumOptionChosen, string valueToPassIn)
    {
        return "something";
    }
}

Firstly, declare your enum outside of the interface/concrete. 首先,在界面/具体之外声明你的枚举。

Then in your interface: 然后在你的界面中:

MyEnum SomeNum {get; set;}

Then in your class: 然后在你的班上:

public MyEnum SomeNum 
{
   get
   {
     ...
   }
   set
   {
      ...
   }
}

Your main problem was you were trying to declare a return type of "Enum" from your interface, when you should be declaring a return type of "MyEnum". 你的主要问题是当你应该声明返回类型“MyEnum”时,你试图从你的界面声明一个返回类型“Enum”。

Remember, enum is a type. 请记住,枚举是一种类型。 You can't force a class to implement a "type", only properties/methods. 您不能强制类实现“类型”,只能实现属性/方法。

That being said, i'm scratching my head to try and figure out what you are trying to do. 话虽这么说,我正试着弄清楚你想要做什么。

I realize the original post is very old, but this is another approach that is clear and works well. 我意识到原来的帖子已经很老了,但这是另一种清晰且运作良好的方法。 Since the Enum is public, just go ahead and define it outside of the concrete class and the interface. 由于Enum是公共的,因此请继续在具体类接口之外定义它。 (I've taken the liberty of renaming the enum type to TheEnum to avoid confusion with the property MyEnum.) (我冒昧地将枚举类型重命名为TheEnum,以避免与属性MyEnum混淆。)

public enum TheEnum
{
    FirstOption,
    SecondOption,
    ThirdOption
}

Then reference it in your interface and concrete class. 然后在您的界面和具体类中引用它。

public interface IThing
{
    TheEnum MyEnum { get; set; }
    string DoAction(TheEnum enumOptionChosen, string valueToPassIn);
}

public class Thing : IThing
{
    public TheEnum MyEnum { get; set; }

    public string DoAction(TheEnum enumOptionChosen, string valueToPassIn)
    {
        return "Something";
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM