Question

It compiles perfectly fine but won't let me access the method when I have an enumeration array. I have another function that extends a single enum value and it works perfectly on any type of enum and shows up via intellisense but not on enum arrays. Can I not do this or something, it works perfectly fine with string and integer arrays

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

***Update this is what I changed it too you can't cast a defined enumeration array to 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;
    }
  }
Was it helpful?

Solution

Try changing the code to something like

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 ...
}

And then using it with

public enum TADA
{
    Foo,
    Bar
}

public class TADA_NON_ENUM
{
}

public struct TADA_STRUCT
{
}

gives

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

EDIT

Declared in an extension class, and used in another.

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 ...
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top