【问题标题】:Why will this compile but not allow me to access the function as an extension C#为什么这会编译但不允许我作为扩展 C# 访问该函数
【发布时间】:2013-12-30 07:17:52
【问题描述】:

它编译得非常好,但是当我有一个枚举数组时不会让我访问该方法。我有另一个扩展单个枚举值的函数,它可以在任何类型的枚举上完美运行,并通过智能感知显示,但不适用于枚举数组。我可以不这样做吗,它与字符串和整数数组完美配合

public static class MyExtensions
{
  public static void WriteCompressed (this Enum[] towrite, Stream output)
  {
    .... Function Code ....
  }
}

***更新这也是我更改的内容,您不能将定义的枚举数组转换为 Enum[]

public static class EnumExtensions 
{
     public static void WriteCompressedEnums<T>(this T[] towrite, Stream output) where T : struct, IConvertible
    {
        if (towrite == null)
        {
            Serialization.ObjectConversion.writeCompressedInt(output, 0);
            return;
        }
        towrite.Length.WriteCompressed(output);
        Type enumtype = Enum.GetUnderlyingType(typeof(T));
        for (int i = 0; i < towrite.Length; i++)
        {
            Object value = Convert.ChangeType(towrite, enumtype);
            long written;
            if (value is ulong)
            {
                written = (long)((ulong)value);
            }
            else
            {
                written = (long)Convert.ChangeType(value, typeof(long));
            }
            written.WriteCompressed(output);
        }
        return;
    }
  }

【问题讨论】:

  • 您是否在要使用此扩展方法的类中包含正确的命名空间?你的班级是静态的吗?
  • @KevinBrechbühl 是的,我用于编写单个压缩枚举的另一个函数与 this 在同一个类中,我在需要调用它的同一个调用函数中使用它。它也是最新编译的
  • 它对我有什么作用? i.imgur.com/Ji6LDnd.png
  • @Selman22 将 Enum[] 更改为自定义或已定义的枚举数组,而不是继承的基类
  • 您能提供您的自定义课程代码吗?我不明白你是什么意思custom or already defined enum array枚举类不能被继承

标签: c# enums extension-methods intellisense enumeration


【解决方案1】:

尝试将代码更改为类似

public static void WriteCompressed<T>(this T[] towrite, Stream output) where T : struct, IConvertible
{
    if (!typeof(T).IsEnum)
    {
        throw new ArgumentException("T must be an enumerated type");
    }
    //.... Function Code ...
}

然后使用它

public enum TADA
{
    Foo,
    Bar
}

public class TADA_NON_ENUM
{
}

public struct TADA_STRUCT
{
}

给予

TADA[] t = new TADA[1];
t.WriteCompressed(new MemoryStream()); //just fine
TADA_NON_ENUM[] tne = new TADA_NON_ENUM[1];
tne.WriteCompressed(new MemoryStream()); //compile time error
TADA_STRUCT[] ts = new TADA_STRUCT[1];
ts.WriteCompressed(new MemoryStream()); //compile time error

编辑

在扩展类中声明,并在另一个类中使用。

public enum TADA
{
    Foo,
    Bar
}

public class TADA_NON_ENUM
{
}

public struct TADA_STRUCT
{
}

public class MyClass
{
    public void Test()
    {
        TADA[] t = new TADA[1];
        t.WriteCompressed(new MemoryStream()); //just fine
        TADA_NON_ENUM[] tne = new TADA_NON_ENUM[1];
        tne.WriteCompressed(new MemoryStream()); //compile time error
        TADA_STRUCT[] ts = new TADA_STRUCT[1];
        ts.WriteCompressed(new MemoryStream()); //compile time error
    }
}

public static class ExtensionClass
{
    public static void WriteCompressed<T>(this T[] towrite, Stream output) where T : struct, IConvertible
    {
        if (!typeof(T).IsEnum)
        {
            throw new ArgumentException("T must be an enumerated type");
        }
        //.... Function Code ...
    }
}

【讨论】:

  • 如果可能的话,我想让它成为一个扩展,如果不可能的话,最后的手段。如果我调用扩展所在的类但仍然不允许我调用它,它会显示在智能感知上。
  • 你试过上面指定的用法了吗? TADA[] t = 新 TADA[1]; t.WriteCompressed(new MemoryStream());
  • 不,但我知道它会起作用。我只是对想要这个作为扩展很挑剔。更糟的是更糟我会接受这个。我知道这一切都差不多,只是我真的很挑剔。
  • 为什么说这不是扩展方法?您可以在上面的编辑中声明它。
  • 哦,好吧,我没有注意到,我只是不希望它出现在我创建的任何类型的每个数组中。我想我可以将函数命名为 WriteCompressedEnums。现在必须这样做,似乎问题是该语言无法将已定义的枚举数组转换为 Enum []。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-01
  • 1970-01-01
  • 2014-10-17
  • 2014-09-14
  • 1970-01-01
相关资源
最近更新 更多