Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多个枚举或泛型枚举_C#_Enums - Fatal编程技术网

C# 多个枚举或泛型枚举

C# 多个枚举或泛型枚举,c#,enums,C#,Enums,我在我的应用程序中为通知创建了多个枚举。每个枚举都绑定到一个模块。我这样做是因为如果我使用一个枚举,它会变得太大。因此,模块1通知类型、模块2通知类型等 我希望有一个单一的通知方法来接收通知类型。是否有一种方法可以接收单个通用枚举值,并将其转换为适当的模块枚举(如下所示) public void sendNotification(GenericEnum notificationType) { // Try to convert the generic enum to module1Noti

我在我的应用程序中为通知创建了多个枚举。每个枚举都绑定到一个模块。我这样做是因为如果我使用一个枚举,它会变得太大。因此,模块1通知类型、模块2通知类型等

我希望有一个单一的通知方法来接收通知类型。是否有一种方法可以接收单个通用枚举值,并将其转换为适当的模块枚举(如下所示)

public void sendNotification(GenericEnum notificationType)
{
   // Try to convert the generic enum to module1Notification or module2Notification
}
或者我必须期望多个枚举值,并将未使用的值设置为“未定义”,以便跳过它们

public void sendNotification(module1NotificationTypes mod1, module2NotificationTypes mod2)
{
   if(mod1 != Module1Notifications.Undefined)
   {
      // We know we received module 1 notification request
   }
   else if(mod2 != Module2Notifications.Undefined)
   {
      // We know we received module 2 notification request
   }
}

我不认为有一种方法可以像你所想的那样枚举枚举。MSDN上的这篇文章可能是处理您的场景的最简洁的方式。有关更多信息,请访问以下网站

编辑1:

链接中给出的示例有两种枚举类型(Alpha和Beta)和一种获取枚举值的方法。我已将该代码的片段包括在下面:

枚举:

public enum Alpha { One, Two, Three }
public enum Beta { Four, Five, Six }
存取器方法:

public T GetOne<T>(object o){
    T one = (T)Enum.Parse(typeof(T), o.ToString());
    return one;
}
public T GetOne(对象o){
tone=(T)Enum.Parse(typeof(T),o.ToString());
返回一个;
}
使用以下方法:

MessageBox.Show(GetOne<Alpha>(Alpha.One).ToString());
MessageBox.Show(GetOne(Alpha.One.ToString());

为什么要拆分枚举?您正在失去枚举的好处

public void sendNotification(NotificationEnum notificationType)
{
   switch(notificationType)
   {
       case NotificationEnum.module_1:
             // We know we received module 1 notification request
       break;
       case NotificationEnum.module_2:
            // We know we received module 2 notification request
       break;
   }
}
如果可以同时具有多个类型,则可以使用[Flags]标记枚举

您甚至可以使用字典将每个枚举值映射到某个函数,我很快就会更新我的答案

可以这样定义枚举。请注意,默认情况下,您最多只能使用
32
值。如果使用
ulong
,则最多只能使用
64
个值

[Flags]
public enum NotificationEnum : ulong
{
    module_1 = 1<<0,
    module_2 = 1<<1,
    module_3 = 1<<2, 
    // and so on
}

您可以使用方法的重载技术

public void sendNotification(module1NotificationTypes mod)
{
   //handle notification type
}

public void sendNotification(module2NotificationTypes mod)
{

}
此外,您还可以对枚举的值范围进行一些肮脏的破解和拆分

enum module1NotificationTypes
{
   Type1Test1 = 1,
   Type1Test2 = 2,
   ...
}

enum module2NotificationTypes
{
   Type2Test1 = 101,
   Type2Test2 = 102,
}
然后创建一个接受
int
或枚举使用的其他整数类型的方法(因为所有枚举在编译时都是数字)。但它可能比简单的重载方法慢。另一方面,编译器可以优化这种行为,而不进行转换

public void sendNotification(int typeNum){
   if (typeNum <= 100)
   {  
      if (!Emum.IsDefined(typeof(module1NotificationTypes), typeNum))
      { 
         throw new ArgumentException();
      }
      //DO Processing
   }
   else 
   {
      if (!Emum.IsDefined(typeof(module2NotificationTypes), typeNum))
      { 
         throw new ArgumentException();
      }
      //DO Processing
   }
}
公共无效发送通知(int-typeNum){

if(typeNum没有通用枚举类型,但可以使用枚举位于整数类型之上的事实(如果未指定任何内容,则为
Int32
)。这使它们可以轻松转换为基础类型。如果它们共享一个类型,也可以轻松地相互转换

如果不存在一组共享的枚举值,则您的
sendNotification
方法将没有多大意义。您可以在枚举中定义它们(可能使用显式值以避免冲突):

定义模块枚举时,可以参考:

public enum module1Notification {
    Undefined = NotificationShared.Undefined
}

public enum module2Notification {
    Undefined = NotificationShared.Undefined
}
同样,没有通用的方法来处理这些枚举,但现在您至少有了通用值,并且可以保证转换将产生语义相同的值。如果您只想使用一种方法,那么当您使用具有值类型约束的通用方法时,您只能避免使用
对象
参数装箱:

public void sendNotification<T>(T notificationType) where T : struct
{
   // Convert to shared enum type
   NotificationShared notification = (NotificationShared)notificationType;
}

这可能不是最好的,但这是处理这种情况的最好方法。

这就是重载的作用。每个枚举类型只有一个方法。从外部看,它看起来像一个单一的方法。你能提供一些描述吗?在帖子中解释了什么?仅仅将用户重定向到帖子是不好的做法,因为帖子可以删除!正如我所说的在原始帖子中提到,一个枚举太大。条目太多,难以管理。您的解决方案不起作用。我考虑过类似的问题,但在这个解决方案中,实际的枚举没有收到。例如,module1NotificationTypes.DoSomething。您只发送枚举类型,而不是notificationType。@Sam看到更新了吗e、 您还可以使用
Action
Func
或其他类型的委托来代替操作。它非常灵活。您仍然可以为不同的模块使用不同的字典,并具有多个枚举。这确实是一个很好的解决方案,因为它可以降低圈复杂度。@ErikPhilips:它可能会降低圈复杂度,但是其他所有的复杂度呢它引入的复杂性?这不是一个明显的构造,因此在智力上比一个更简单的解决方案更难处理。@Sefe you’s point是有效的,但没有任何细节,我无法评论。我不知道为什么单个枚举比多个枚举更复杂。这里没有一个答案是直接基于t的如果这是我的问题,我肯定会基于这个问题的难度编写单元测试,并且我会尽可能少地编写测试代码(这就是圈复杂度发挥作用的地方)。
public void sendNotification(int typeNum){
   if (typeNum <= 100)
   {  
      if (!Emum.IsDefined(typeof(module1NotificationTypes), typeNum))
      { 
         throw new ArgumentException();
      }
      //DO Processing
   }
   else 
   {
      if (!Emum.IsDefined(typeof(module2NotificationTypes), typeNum))
      { 
         throw new ArgumentException();
      }
      //DO Processing
   }
}
public enum NotificationShared {
    Undefined = 0
}
public enum module1Notification {
    Undefined = NotificationShared.Undefined
}

public enum module2Notification {
    Undefined = NotificationShared.Undefined
}
public void sendNotification<T>(T notificationType) where T : struct
{
   // Convert to shared enum type
   NotificationShared notification = (NotificationShared)notificationType;
}
public void sendNotification(NotificationShared notificationType)
{
    // Use shared enum type
}

public void sendNotification(module1Notification notificationType)
{
    sendNotification((NotificationShared)notificationType);
}

public void sendNotification(module2Notification notificationType)
{
    sendNotification((NotificationShared)notificationType);
}