Domanda

In vs2008, è possibile scrivere un metodo di estensione che si applicherebbe a qualsiasi enumerazione.

So che puoi scrivere metodi di estensione su una specifica enumerazione, ma voglio essere in grado di ogni enumerazione usando un singolo metodo di estensione. È possibile?

È stato utile?

Soluzione

Sì, basta codificare con il tipo Enum di base, ad esempio

public static void Something(this Enum e)
{
    // code here
}

Il lato negativo è che probabilmente finirai per fare cose piuttosto brutte come trovare il vero tipo di base usando Enum.GetUnderlyingType , lanciando e scendendo diversi rami a seconda del tipo di base dell'enum, ma puoi trovare alcuni buoni utilizza per questo (ad esempio abbiamo metodi IsOneOf e IsCombinationOf che si applicano a tutti gli enumeratori).

PS: ricorda quando scrivi il metodo che, sebbene sconsigliato, puoi usare float e double come tipi di base per gli enum, quindi avrai bisogno di alcuni casi speciali sia per quei valori non firmati.

Altri suggerimenti

Sì, puoi. Il tipo di estensione target è di tipo Enum . In C #, questo sarebbe fatto come:

public static void EnumExtension(this Enum e)
{
}

o in questo modo in VB:

<Extension()> _
Public Sub EnumExtension(ByVal s As Enum)
End Sub

FYI Ecco un ottimo esempio di un metodo di estensione Enum che sono stato in grado di utilizzare. Implementa una funzione TryParse () senza distinzione tra maiuscole e minuscole per enums:

public static class ExtensionMethods
{
    public static bool TryParse<T>(this Enum theEnum, string strType, 
        out T result)
    {
        string strTypeFixed = strType.Replace(' ', '_');
        if (Enum.IsDefined(typeof(T), strTypeFixed))
        {
            result = (T)Enum.Parse(typeof(T), strTypeFixed, true);
            return true;
        }
        else
        {
            foreach (string value in Enum.GetNames(typeof(T)))
            {
                if (value.Equals(strTypeFixed, 
                    StringComparison.OrdinalIgnoreCase))
                {
                    result = (T)Enum.Parse(typeof(T), value);
                    return true;
                }
            }
            result = default(T);
            return false;
        }
    }
}

Lo useresti nel modo seguente:

public enum TestEnum
{
    A,
    B,
    C
}

public void TestMethod(string StringOfEnum)
{
    TestEnum myEnum;
    myEnum.TryParse(StringOfEnum, out myEnum);
}

Ecco i due siti che ho visitato per trovare questo codice:

Try Insensitive TryParse for enumerazioni

Metodi di estensione per Enum

Ecco un altro esempio - anche IMHO più bello del dover creare e inizializzare una variabile temp.

public static class ExtensionMethods 
{
    public static void ForEach(this Enum enumType, Action<Enum> action)
    {
        foreach (var type in Enum.GetValues(enumType.GetType()))
        {
            action((Enum)type);
        }
    }
}

public enum TestEnum { A,B,C } 
public void TestMethod() 
{
    default(TestEnum).ForEach(Console.WriteLine); 
} 

Puoi anche implementare il metodo di conversione come segue:

public static class Extensions
{
    public static ConvertType Convert<ConvertType>(this Enum e)
    {
        object o = null;
        Type type = typeof(ConvertType);

        if (type == typeof(int))
        {
            o = Convert.ToInt32(e);
        }
        else if (type == typeof(long))
        {
            o = Convert.ToInt64(e);
        }
        else if (type == typeof(short))
        {
            o = Convert.ToInt16(e);
        }
        else
        {
            o = Convert.ToString(e);
        }

        return (ConvertType)o;
    }
}

Ecco un esempio di utilizzo:

int a = MyEnum.A.Convert<int>();

A volte è necessario convertirsi da un enum all'altro, in base al nome o al valore dell'enum. Ecco come si può fare bene con i metodi di estensione:

enum Enum1 { One = 1, Two = 2, Three = 3 };
enum Enum2 { Due = 2, Uno = 1 };
enum Enum3 { Two, One };

Enum2 e2 = Enum1.One.ConvertByValue<Enum2>();
Enum3 e3 = Enum1.One.ConvertByName<Enum3>();
Enum3 x2 = Enum1.Three.ConvertByValue<Enum3>();

public static class EnumConversionExtensions
{
    public static T ConvertByName<T>(this Enum value)
    {
        return (T)Enum.Parse(typeof(T), Enum.GetName(value.GetType(), value));
    }

    public static T ConvertByValue<T>(this Enum value)
    {
        return (T)((dynamic)((int)((object)value)));
    }
}

Un altro esempio di creazione dell'estensione Enum, ma questa volta restituisce il tipo di input enum.

public static IEnumerable<T> toElementsCollection<T>(this T value) where T : struct, IConvertible
    {
        if (typeof(T).IsEnum == false) throw new Exception("typeof(T).IsEnum == false");

        return Enum.GetValues(typeof(T)).Cast<T>();
    }

Esempio di utilizzo:

public enum TestEnum { A,B,C };

TestEnum.A.toElementsCollection();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top