tags:

views:

606

answers:

7
+3  A: 

Unfortunately you cannot. Extension methods are a feature which allows you to have the appearance of calling a new method on an instance of a type. It has no capacity to give this capability to types themselves.

When I want to add a set of methods add type level I usually create a new static class called OriginalTypeNameUtil and add the methods there. For example ResponseUtil. This way the class name is visible when I type the original type name.

JaredPar
The reason for the extension method is that I want to apply it to all enums and don’t want to create lots of helper classes. I know I could create an EnumHelper class with the method there but was trying to group it with all my other enum extensions methods I have written.
Cragly
+1  A: 

It seems like you just want a real static method and an "enum-like" class:

public class Response
{
    public static readonly Response Yes = new Response(1);
    public static readonly Response No = new Response(2);
    public static readonly Response Maybe = new Response(3);

    int value;

    private Response(int value)
    {
        this.value = value;
    }

    public static DataTable ToDataSource()
    {
        // ...
    }
}
bobbymcr
Thanks everybody for the quick responses but unfortunately I have to use enums as they are used throughout the application and deviating away from this standard just to get some desired functionality would not be approved.
Cragly
A: 
string[] names = Enum.GetNames(typeof(Test));

Array test = Enum.GetValues(typeof(Test));
var vals = test.Cast<int>();

var source = from n in names
             from v in vals
             select new
             {
                 Text = n,
                 Value = v
             };

Not tested but was quick :)

MiG
How does this relate to his question?
John Fisher
That'll teach me to read all of the question! :) First reply on stackoverflow too! oops :P
MiG
A: 

If you use a struct that emulates an enum:

   public struct Response
   {
     private int ival;
     private Response() {} // private ctor to eliminate instantiation
     private Response(int val) { ival = val; }
     private static readonly Response Yes = new Response(1);
     private static readonly Response No = new Response(2);
     private static readonly Response Maybe= new Response(3);
     // etc...  ...for whatever other functionality you want.... 
   }

Then this struct functions exactly (close to exactly !) like an enum, and you can add extensiuon methods to it...

Charles Bretana
+1  A: 

It is not possible to create an extension method on an enum.

You could however create a generic function that accept enums and create a table from it.

public static DataTable CreateDataSource<TEnum>()
{
    Type enumType = typeof(TEnum);

    if (enumType.IsEnum) // It is not possible to do "where TEnum : Enum"
    {
        DataTable table = new DataTable();
        table.Columns.Add("Name");
        table.Columns.Add("Value", enumType);

        foreach (var value in Enum.GetValues(enumType))
        {
            table.Rows.Add(Enum.GetName(enumType, value), value);
        }

        return table;
    }
    else
        throw new ArgumentException("Type TEnum is not an enumeration.");
}
Pierre-Alain Vigeant
A: 

Remember that if you choose to go the route of implementing a 'Enum-Class' and find yourself in a position where the constructor isnt allowed to be private, you'll need to override Equals and GetHashCode.

JeffreyABecker
+2  A: 

I'm doing something similar only, instead of extending the Enum type, I extended the DataTable .

public static DataTable FromEnum(this DataTable dt, Type enumType) {
if (!enumType.IsEnum) { throw new ArgumentException("The specified type is not a System.Enum."); }
DataTable tbl = new DataTable();
string[] names = Enum.GetNames(enumType);
Array values = Enum.GetValues(enumType);
List<string> enumItemNames = new List<string>();
List<int> enumItemValues = new List<int>();
try {
 // build the table
 tbl.Columns.Add(new DataColumn("Value", typeof(string)));
 tbl.Columns.Add(new DataColumn("Text", typeof(string)));
 // Get the enum item names (using the enum item's description value if defined)
 foreach (string enumItemName in names) {
  enumItemNames.Add(((Enum)Enum.Parse(enumType, enumItemName)).ToDescription());
 }
 // Get the enum item values
 foreach (object itemValue in values) {
  enumItemValues.Add(Convert.ToInt32(Enum.Parse(enumType, itemValue.ToString())));
 }
 // Make sure that the data table is empty
 tbl.Clear();

 // Fill the data table
 for (int i = 0; i <= names.Length - 1; i++) {
  DataRow newRow = tbl.NewRow();
  newRow["Value"] = enumItemValues[i];
  newRow["Text"] = enumItemNames[i];
  tbl.Rows.Add(newRow);
 }
}
catch {
 tbl.Clear();
 tbl = dt;
}
// Return the data table to the caller
return tbl;

}

and it's called like this:

DataTable tbl = new DataTable().FromEnum(typeof(YourEnum));
Ken