本帖最后由 pthiiu 于 2010-10-13 16:04:39 编辑

解决方案 »

  1.   

    enum 就是 int, 把参数改成 int 就是了
      

  2.   

    public void Operation<T>() where T : enum
    {
        ....
    }
    看样子好象对,偶也概念不强了.
      

  3.   

    嗯????我试试
    public enum MyEnum{Zero, One, Two, Three };public void foo<T>(T value) where T : int
    {
        Console.WriteLine("It is {0}", value);
    }public void MyMethod()
    {
        foo<MyEnum>(MyEnum.One);
    }
    这样会正确显示  It is One.  吗?  怎么感觉这么怪呢?
      

  4.   


    I tried in both way. Neither works. Anyone know how to work with it? Simpler, better.
      

  5.   

    不懂,where T : struct 可行吗
      

  6.   

    应该是这样吧
    public void Operation<T>() where T : struct
    {
        ....
    }泛型约束  class  表示引用类型  struct 表示值类型
      

  7.   

    public void foo<T>(T value) where T : int=>public void foo<T>(T value) where T : struct
      

  8.   


    用struct先,再加个判断
    public void Operation<T>() where T : struct
    {
        if(!typeof(T).IsEnum) throw new ArgumentException("必须是枚举类型");
    }
      

  9.   

    where T : struct是可以的,但是使用不了enum的扩展方法了。
      

  10.   


    public enum MyEnum { Zero, One, Two, Three };public void MyMethod()
    {
        foo<MyEnum>(MyEnum.One);
    }public void foo<T>(T value)
    {
        Type enumType = typeof(T);    if (enumType.BaseType != typeof(Enum))
            throw new ArgumentException("T must be of type System.Enum");    Console.WriteLine((T)Enum.Parse(enumType, value.ToString()));
        Console.WriteLine();
    }
    got it.  but Can I use: ?
    switch ((T)value) 

        case enumType.0:
            ....
            break;
        case enumType.1:
            ....
            break;
        ....
        default:
            break;
    }
    Or similar
      

  11.   

    Enum.IsDefined(typeof(T), value)是不是这个意思?
      

  12.   

    public T GetEnumFromString<T>(string value) where T : struct, IConvertible 

       if (!typeof(T).IsEnum)  
       { 
       } 

    http://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum