【问题标题】:Why can I not "see" this enum extension method?为什么我不能“看到”这个枚举扩展方法?
【发布时间】:2010-07-20 07:07:57
【问题描述】:

为什么我看不到这个枚举扩展方法? (我想我快疯了)。

文件1.cs

namespace Ns1
{
    public enum Website : int
    {
        Website1 = 0,
        Website2
    }
}

文件2.cs

using Ns1;

namespace Ns2
{
    public class MyType : RequestHandler<Request, Response>
    {                        
        public override Response Handle(Request request,                                       CRequest cRequest)
        {
            //does not compile, cannot "see" ToDictionary
            var websites = Website.ToDictionary<int>(); 

            return null;
        }
    }


    //converts enum to dictionary of values
    public static class EnumExtensions
    {        
        public static IDictionary ToDictionary<TEnumValueType>(this Enum e)
        {                        
            if(typeof(TEnumValueType).FullName != Enum.GetUnderlyingType(e.GetType()).FullName) throw new ArgumentException("Invalid type specified.");

            return Enum.GetValues(e.GetType())
                        .Cast<object>()
                        .ToDictionary(key => Enum.GetName(e.GetType(), key), 
                                      value => (TEnumValueType) value);            
        }
    }
}

【问题讨论】:

    标签: c# extension-methods enums


    【解决方案1】:

    您试图将扩展方法作为该类型的静态方法调用,而不是作为该类型对象的实例方法。不支持这种扩展方法的使用。

    如果你有一个实例那么扩展方法被找到:

    Website website = Website.Website1;
    var websites = website.ToDictionary<int>();
    

    【讨论】:

    • 应该不是WebSite website = WebSite.Website1;是一个枚举?
    • @btlog:两者都有效。在这种情况下,他没有使用实际值,所以没有区别。
    • @btlog:new Website() 给出与Website.Website1 相同的结果,因为它返回默认值,即基础值为 0 的值。但我并不推荐这种语法:-)跨度>
    • 感谢@Mark 和@Hans。我没有意识到这是有效的。今天学到了新东西,现在是上午 9 点,也许我现在应该回家了 :)
    【解决方案2】:

    this Enum e 指的是一个枚举实例,而网站实际上是一个枚举类类型。

    【讨论】:

      【解决方案3】:

      扩展方法只是syntactic sugaronly work with instances and not with the type。因此,您必须在 Website 类型的实例上调用扩展方法,而不是在类型本身上调用,正如 Mark 所提到的。

      供您参考,除了马克所说的,代码在编译时转换如下。

      //Your code
      Website website = new Website();
      var websites = website.ToDictionary<int>();
      
      
      //After compilation.
      Website website = new Website();
      var websites = EnumExtensions.ToDictionary<int>(website);
      

      扩展方法的improved version 将仅扩展类型网站而不是枚举。

      //converts enum to dictionary of values
      public static class EnumExtensions
      {        
          public static IDictionary ToDictionary<TEnumValueType>(this Website e)
          {                        
              if(typeof(TEnumValueType).FullName != Enum.GetUnderlyingType(e.GetType()).FullName) throw new ArgumentException("Invalid type specified.");
      
              return Enum.GetValues(e.GetType())
                          .Cast<object>()
                          .ToDictionary(key => Enum.GetName(e.GetType(), key), 
                                        value => (TEnumValueType) value);            
          }
      }
      

      【讨论】:

        【解决方案4】:

        您需要更改扩展方法的签名以使用您的枚举,而不是枚举类型本身。即,将扩展方法签名中的Enum 更改为Website

        public static IDictionary ToDictionary<TEnumValueType>(this Website enum, ...)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-09-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-06
          • 2021-05-06
          • 1970-01-01
          相关资源
          最近更新 更多