在各个枚举常量上定义 Function

发布于 2024-12-04 17:03:10 字数 458 浏览 1 评论 0原文

我的一个类将枚举作为其属性之一,该类的定义如下所示:

public class A
{

    string properrty1;
    int id;
    CustomEnum enum;

    public A()
    {
      //initialize the properties

    }

}

因此,基于枚举类型,我需要该类来调用一个函数,该函数将访问该类的一些属性并执行操作与它。

最好的方法是什么?

我正在考虑将接口作为类的属性之一,然后根据枚举在类的构造函数中实例化接口,并且在需要时我可以执行 classObject.interface.interfaceImplementation();

这听起来是个好方法吗?如果是,您可以建议一种初始化接口的好方法,除了使用一堆 switch case 语句来实例化该标记类型的接口的适当实现之外。

one of my class has enum as one of its properties and the definition of the class looks like this:

public class A
{

    string properrty1;
    int id;
    CustomEnum enum;

    public A()
    {
      //initialize the properties

    }

}

Thus based on the enum type the class I need this class to call a function that will access some of the properties of the class and perform actions with it.

What would be the best approach for that?

I was thinking of having an interface as one of the properties of the class and then instantiate the interface in the constructor of the class based on the enum and when needed I can do classObject.interface.interfaceImplementation();

Does that sound like a good approach? If yes can you suggest a good way to initialize the interface except for using a bunch of switch case statements that will instantiate the appropriate implementation of the interface for that tagtype.

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

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

发布评论

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

评论(2

牵你的手,一向走下去 2024-12-11 17:03:10

一种选择是使用 Dictionary

private static readonly Dictionary<CustomEnum, Action<A>> Actions =
{
    { CustomEnum.Foo, FooAction },
    { CustomEnum.Bar, BarAction },
    { CustomEnum.Baz, BazAction },
};

private static void FooAction(A a) { ... }
private static void BarAction(A a) { ... }
private static void BazAction(A a) { ... }

public void DoSomething()
{
    Actions[enumValue](this);
}

这实际上取决于这些函数的用途等。如果操作确实是枚举本身固有的,您可能需要创建一个“智能枚举”,它基本上是一组固定的值,但它是一个具有行为的。这有点像 Java 枚举,但 C# 作为一种语言并没有太多支持。

请参阅 DateTimeFieldType野田时间就是一个例子。它只有属性,它可以很容易地具有方法,并且它们可以是抽象的,其中嵌套有具体的私有子类 - 有点像ZoneLocalMapping,也在野田时间内 - 但在这种情况下它是具有三种不同实现而不是三个特定值的工厂。

One option would be to have a Dictionary<CustomEnum, Action>:

private static readonly Dictionary<CustomEnum, Action<A>> Actions =
{
    { CustomEnum.Foo, FooAction },
    { CustomEnum.Bar, BarAction },
    { CustomEnum.Baz, BazAction },
};

private static void FooAction(A a) { ... }
private static void BarAction(A a) { ... }
private static void BazAction(A a) { ... }

public void DoSomething()
{
    Actions[enumValue](this);
}

It really depends on what these functions are meant to do, etc. If the action is really inherent in the enum itself, you may want to create a "smart enum" which is basically a fixed set of values, but it's a class with behaviour. This is a bit like a Java enum, but C# as a language doesn't have much support.

See DateTimeFieldType in Noda Time for an example of this. That only has properties, it could easily have methods, and they could be abstract with concrete private subclasses nested within there - a bit like ZoneLocalMapping, also within Noda Time - but in that case it's a factory with three different implementations instead of three specific values.

只有影子陪我不离不弃 2024-12-11 17:03:10

首先,switch 语句有什么问题?如果您所做的只是调用一个函数,只需在其中放置一个开关并适当地调用它 - 不需要接口或其他任何东西。不要过度设计它。

现在,如果您有充分的理由不采用这种方法,则另一种切换方法是 Dictionary 其中 T 适合您需要执行的操作。

First off, what's wrong with switch statements? If all you do is call a function, just put a switch in there and call it appropriately -- there's no need for interfaces or anything else. Don't overengineer it.

Now if you have good reason to not go with this approach, the other alternative to switch is a Dictionary<CustomEnum, T> where T is something appropriate to what you need to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文