开发者

How do I make a method work for any enum

I have the following code, where ApplicationType is an enum. I have the same repeated code (e开发者_StackOverflowverything except the parameter types) on many other enums. Whats the best way to consolidate this code?

 private static string GetSelected(ApplicationType type_, ApplicationType checkedType_)
    {
        if (type_ == checkedType_)
        {
            return " selected ";
        }
        else
        {
            return "";
        }
    }


Shooting from the hip, something along the lines of...

private static string GetSelected<T>(T type_, T checkedType_) where T : System.Enum{
  //As before
}

Apparently that's illegal.

To simply cut down on repetition, you could just replace T with Enum thusly,

private static String GetSelected(Enum type_, Enum checkedType_){
  if(type_.CompareTo(_checkedType) == 0) return "selected";

  return "";
}

Though this doesn't get you much in the way of type safety, as two different enumeration types could be passed in.

You could instead fail at runtime:

private static String GetSelected(Enum type_, Enum checkedType_){
   if(type_.GetType() != checkedType.GetType()) throw new Exception();
   //As above
}

To get some compile time safety you could use a generic constraint, though since you can't using the Enum class you won't be able to restrict everything:

private static String GetSelected<T>(T type_, T checkedType_) where T : IComparable, IFormattable, IConvertible{
  if(!(first is Enum)) throw new Exception();
  //As above
}

The above will make sure you only pass Enums of the same type (you'll get a type inference error if you pass in two different Enums) but will compile when non-Enum's that meet the constraints on T are passed.

There doesn't seem to be a great solution to this problem out there, unfortunately.


For equality on an emum? Simply:

public static string GetSelected<T>(T x, T y) {
    return EqualityComparer<T>.Default.Equals(x,y) ? " selected " : "";
}

You could add where T : struct to the top line, but there isn't a need to do that.

For a full example including demo:

using System;
using System.Collections.Generic;
static class Program {
    static void Main() {
        ApplicationType value = ApplicationType.B;
        Console.WriteLine("A: " + GetSelected(value, ApplicationType.A));
        Console.WriteLine("B: " + GetSelected(value, ApplicationType.B));
        Console.WriteLine("C: " + GetSelected(value, ApplicationType.C));
    }
    private static string GetSelected<T>(T x, T y) {
        return EqualityComparer<T>.Default.Equals(x, y) ? " selected " : "";
    }
    enum ApplicationType { A, B, C }
}


Is this appropriate for your needs?

private static string GetSelected<T>(T type_, T checkedType_)
    where T: struct
{
    if(type_.Equals(checkedType_))
        return " selected ";
    return "";
}


It's hard to do this through a generic constraint at compile time because an enum is really an int. You'll need to restrict the values at runtime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜