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

发布于 2024-09-28 05:22:08 字数 721 浏览 2 评论 0原文

我有一个接口

接口:

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

具体实现:

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;
    }
  }
}

当我尝试编译它时,我得到“IThing.MyEnum”是一个“属性”,但像“类型”一样使用。 我缺少一些关于能够要求在 DoAction() 签名中使用 Enum 的信息。

感谢您的任何帮助。

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.'
I'm missing something about being able to require use of the Enum in the DoAction() signature.

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑白记忆 2024-10-05 05:22:09

在您的接口中,MyEnum 是变量的名称而不是类型,因此编译器不会识别它。您应该能够使用泛型来完成此任务。

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

然后你可以像这样实现它:

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";
    }
}

In your interface MyEnum is the name of the variable not the type, so it won't be recognized by the compiler. 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";
    }
}
温柔一刀 2024-10-05 05:22:09

首先,在接口/具体之外声明您的枚举。

然后在你的界面中:

MyEnum SomeNum {get; set;}

然后在你的类中:

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

你的主要问题是你试图从你的界面声明“Enum”的返回类型,而你应该声明“MyEnum”的返回类型。

请记住,枚举是一种类型。您不能强制类实现“类型”,只能实现属性/方法。

话虽这么说,我正在挠头试图弄清楚你想做什么。

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".

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.

四叶草在未来唯美盛开 2024-10-05 05:22:09

我意识到原来的帖子很旧了,但这是另一种清晰且效果良好的方法。由于枚举是公共​​的,因此只需在具体类接口之外定义它即可。 (我冒昧地将枚举类型重命名为 TheEnum,以避免与属性 MyEnum 混淆。)

public enum TheEnum
{
    FirstOption,
    SecondOption,
    ThirdOption
}

然后在接口和具体类中引用它。

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";
    }
}

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. (I've taken the liberty of renaming the enum type to TheEnum to avoid confusion with the property 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";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文