Extension method on enumeration, not instance of enumeration - c#

I have an enumeration for my Things like so:
public enum Things
{
OneThing,
AnotherThing
}
I would like to write an extension method for this enumeration (similar to Prise's answer here) but while that method works on an instance of the enumeration, ala
Things thing; var list = thing.ToSelectList();
I would like it to work on the actual enumeration instead:
var list = Things.ToSelectList();
I could just do
var list = default(Things).ToSelectList();
But I don't like the look of that :)
I have gotten closer with the following extension method:
public static SelectList ToSelectList(this Type type)
{
if (type.IsEnum)
{
var values = from Enum e in Enum.GetValues(type)
select new { ID = e, Name = e.ToString() };
return new SelectList(values, "Id", "Name");
}
else
{
return null;
}
}
Used like so:
var list = typeof(Things).ToSelectList();
Can we do any better than that?

Extension methods only work on instances, so it can't be done, but with some well-chosen class/method names and generics, you can produce a result that looks just as good:
public class SelectList
{
// Normal SelectList properties/methods go here
public static SelectList Of<T>()
{
Type t = typeof(T);
if (t.IsEnum)
{
var values = from Enum e in Enum.GetValues(type)
select new { ID = e, Name = e.ToString() };
return new SelectList(values, "Id", "Name");
}
return null;
}
}
Then you can get your select list like this:
var list = SelectList.Of<Things>();
IMO this reads a lot better than Things.ToSelectList().

No.
The best you can do is put it on a static class, like this:
public static class ThingsUtils {
public static SelectList ToSelectList() { ... }
}

Aaronaught's answer is really great, based on that I made the following implementation:
public class SelectList
{
public static IEnumerable<Enum> Of<T>() where T : struct, IConvertible
{
Type t = typeof(T);
if (t.IsEnum)
{
return Enum.GetValues(t).Cast<Enum>();
}
throw new ArgumentException("<T> must be an enumerated type.");
}
}
In my opinion it's a little bit safer, as you can - almost - call it only with Enums, and of course instead of the throw you can simply return null if you want an exception-free version.

I use 'Type' instead of 'Enum' to add extension. Then I can get any type of list back from the method. Here it returns string values:
public static string[] AllDescription(this Type enumType)
{
if (!enumType.IsEnum) return null;
var list = new List<string>();
var values = Enum.GetValues(enumType);
foreach (var item in values)
{
// add any combination of information to list here:
list.Add(string.Format("{0}", item));
//this one gets the values from the [Description] Attribute that I usually use to fill drop downs
//list.Add(((Enum) item).GetDescription());
}
return list.ToArray();
}
Later I could use this syntax to get what I want:
var listOfThings = typeof (Things).AllDescription();

#Aaronaught has a very good answer. To extend his answer, you can also even make it more generic. I have this in a global library...
public static IQueryable GetAllEnumValues<T>()
{
IQueryable retVal = null;
Type targetType = typeof(T);
if(targetType.IsEnum)
{
retVal = Enum.GetValues(targetType).AsQueryable();
}
return retVal;
}
Now you have de-coupled this functionality from the SelectList class. So you can call this in your SelectList methods, or anywhere else for that matter.
public class SelectList
{
public static SelectList Of<T>
{
IQueryable enumValues = GetAllEnumValues<T>();
var values =
from Enum e in enumValues
select new { ID = e, Name = e.ToString() };
return new SelectList(values, "Id", "Name");
}
}

In my opinion, this is the cleanest way. Why?
It works for any System.Enum
The extension method itself is cleaner.
To call it you just add new and that's a small trade off (because it has to have an instance in order to work.
You aren't passing null around and it literally won't compile if you try to use it with another type.
Usage:
(new Things()).ToSelectList()
Extension Method:
[Extension()]
public SelectList ToSelectList(System.Enum source)
{
var values = from Enum e in Enum.GetValues(source.GetType)
select new { ID = e, Name = e.ToString() };
return new SelectList(values, "Id", "Name");
}

The closest you can come, I think, is to dummy things up a bit to work like an enum without being one. Here's what I've come up with--it seems like a lot of work just to plop a static method on an enumeration, although I do understand the programming appeal of it:
public class PseudoEnum
{
public const int FirstValue = 1;
private static PseudoEnum FirstValueObject = new PseudoEnum(1);
public const int SecondValue = 2;
private static PseudoEnum SecondValueObject = new PseudoEnum(2);
private int intValue;
// This prevents instantation; note that we cannot mark the class static
private PseudoEnum() {}
private PseudoEnum(int _intValue)
{
intValue = _intValue;
}
public static implicit operator int(PseudoEnum i)
{
return i.intValue;
}
public static implicit operator PseudoEnum(int i)
{
switch (i)
{
case FirstValue :
return FirstValueObject;
case SecondValue :
return SecondValueObject;
default:
throw new InvalidCastException();
}
}
public static void DoSomething(PseudoEnum pe)
{
switch (pe)
{
case PseudoEnum.FirstValue:
break;
case PseudoEnum.SecondValue:
break;
}
}
}

Related

Set Values in Generic IEnumerable

I have a generic method that I want to pass a variable to, and if that variable is IEnumerable, set all its elements to its default value. This is what I have so far:
public static T set_to_default<T>(T the_obj)
{
var the_enumerable = the_obj as IEnumerable;
if (the_enumerable != null)
{
foreach (var element in the_enumerable)
{
// I don't know what to put here
// I want to set each element to its default value: default(T)
}
return the_obj;
}
else
{
return default(T);
}
}
What do I put inside the foreach loop?
You should just work with overloads, it's much simpler (if I understand your question correctly).
public static T SetToDefault<T>(T the_obj)
{
return default(T);
}
public static IEnumerable<T> SetToDefault<T>(IEnumerable<T> the_enumerable)
{
return the_enumerable.Select(value => default(T));
}
FYI I tested my code with this function:
public static void Test()
{
int myInt = 7;
IEnumerable<int> myEnumberable = new List<int>() { 1, 4, 8, 9 };
myInt = SetToDefault(myInt);
myEnumberable = SetToDefault(myEnumberable);
Console.WriteLine($"MyInt: {myInt}");
Console.WriteLine($"MyEnumberable: {String.Join(", ", myEnumberable)}");
}
To add to this, keep in mind that the name SetToDefault isn't a great choice. When you pass in an int, you will get back an int. You still have to set the value yourself (myInt = SetToDefault(myInt);) which is kind of contradictory to what the name of the function implies.
By the way, note that the first function (T SetToDefault<T>(T the_obj)) has a parameter which is never used. To work around this (to be fair, small) issue, you could use an extension method:
public static class Extensions {
public static T GetDefault<T>(this T value) {
return default(T);
}
}
Note that even here, you will have to set the value to the return of the function. Returning void and simply doing value = default(T); will not work for primitive types like int. That's also why I named it GetDefault instead of SetToDefault this time.
var myWhatever = 3.4;
myWhatever = myWhatever.GetDefault();
Inside the for loop put the following statement:
var default_val = Activator.CreateInstance(element.GetType());
This is how you set to default value
and here is a complete working example of your program
class Program
{
static void Main(string[] args)
{
var my_list = new List<int>() { 1, 2, 3 };
var list2 = set_to_default<List<int>>(my_list);
foreach (var elem in list2)
{
Console.WriteLine(elem);
}
Console.ReadKey();
}
public static T set_to_default<T>(T the_obj)
{
IEnumerable the_enumerable = the_obj as IEnumerable;
if (the_enumerable != null)
{
Type type = the_enumerable.GetType().GetGenericArguments()[0];
Type listType = typeof(List<>).MakeGenericType(new[] { type });
IList list = (IList)Activator.CreateInstance(listType);
var looper = the_enumerable.GetEnumerator();
while (looper.MoveNext())
{
var current_obj = looper.Current;
current_obj = Activator.CreateInstance(current_obj.GetType());
list.Add(current_obj);
}
return (T)list;
}
else
{
return default(T);
}
}
}
I suggest to use lists instead of enumerables because the last ones are immutable. That is once you create them you cannot edit them.
Final thing....if you want to support other types of IEnumerable the are not IList you may use the C# 'is' keyword to figure it out. In this case you need to know the underlying type of your enumerable at run-time. You may do that using the C# 'is' keyword
if (the_enumerable is IList)
{
editableType = typeof(List<>).MakeGenericType(new[] { type });
}
else if(the_enumerable is ICollection)
{
......
}

Generic enum as method parameter

Given a constructor
public MyObject(int id){
ID = id;
}
And two enums:
public enum MyEnum1{
Something = 1,
Anotherthing = 2
}
public enum MyEnum2{
Dodo = 1,
Moustache= 2
}
Is it possible to pass in a generic enum as a parameter of the constructor? I'm looking for a solution along the lines of:
public MyObject(enum someEnum){
ID = (int)someEnum;
}
So you can do:
var newObject = new MyObject(MyEnum1.Something);
var anotherObject = new MyObject(MyEnum2.Dodo);
Another option would be:
public MyObject(Enum someEnum){
ID = Convert.ToInt32(someEnum);
}
This way you can use it like you requested without having to cast to int each time you call your contstructors:
var newObject = new MyObject(MyEnum1.Something);
var anotherObject = new MyObject(MyEnum2.Dodo);
Why do you want to pass the enums, while you could pass integers ?
var newObject = new MyObject((int)MyEnum1.Something);
var anotherObject = new MyObject((int)MyEnum2.Dodo);
and use your first constructor :
public MyObject(int id){
ID = id;
}
Just use a generic constructor:
class MyObject<T> {
public MyObject(T someEnum) where T : struct, IConvertible
{
if (!typeof(T).IsEnum)
throw new ArgumentException("Not an enum");
ID = Convert.ToInt32(someEnum);
}
}
Now you can easily call it like this:
var m = new MyObject<MyEnum>(MyEnum1.Something);
But easier would be to pass the enum as integer to the constructor as mentioned in other answers.
EDIT: As of C# 7.3 you can use an enum-constraint right away:
class MyObject<T> where T: Enum { ... }
Well, if you really need to make this call generic for a wide variety of types, then IMHO you should use:
Type.IsEnum to check if your argument is really an Enum;
Enum.GetUnderlyingType to know what type is your argument is based on (it's not necessarily an Int32);
Now cast your object.
public static Int32 GetAnInt<T>(T arg) where T : struct
{
if ((typeof(T).IsEnum))
{
var underlyingType = typeof(T).GetEnumUnderlyingType();
if (underlyingType == typeof(Int32)
|| underlyingType == typeof(Int16)) //etc.
{
try
{
dynamic value = arg;
var result = (Int32)value; // can throw InvalidCast!
return result;
}
catch
{
throw;
}
}
else
{
throw new InvalidCastException("Underlying type
is certainly not castable to Int32!");
}
}
else
{
throw new InvalidCastException("Not an Enum!");
}
}
That way you achieve the beautiful syntax of: var j = GetAnInt(MyEnum.FirstValue);
Are you using properties enum or parameters.
public enum Enum1{}
public Enum1 enum1 { get;set; }
public MyObject()
{
ID = (int)enum1;
}
Just try it. I hope it is useful.

Generic Enum to SelectList extension method

I need to create a SelectList from any Enum in my project.
I have the code below which I create a select list from a specific enum, but I'd like to make an extension method for ANY enum. This example retrieves the value of the DescriptionAttribute on each Enum value
var list = new SelectList(
Enum.GetValues(typeof(eChargeType))
.Cast<eChargeType>()
.Select(n => new
{
id = (int)n,
label = n.ToString()
}), "id", "label", charge.type_id);
Referencing this post, how do I proceed?
public static void ToSelectList(this Enum e)
{
// code here
}
What I think you are struggling with, is the retrieval of the description. I'm sure once you have those that you can define your final method which gives your exact result.
First, if you define an extension method, it works on a value of the enum, not on the enum type itself. And I think, for easy of usage, you would like to call the method on the type (like a static method). Unfortunately, you cannot define those.
What you can do is the following. First define a method which retrieves the description of the enum value, if it has one:
public static string GetDescription(this Enum value) {
string description = value.ToString();
FieldInfo fieldInfo = value.GetType().GetField(description);
DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0) {
description = attributes[0].Description;
}
return description;
}
Next, define a method which takes all values of an enum, and use the previous method to look up the value which we want to show, and return that list. The generic argument can be inferred.
public static List<KeyValuePair<TEnum, string>> ToEnumDescriptionsList<TEnum>(this TEnum value) {
return Enum
.GetValues(typeof(TEnum))
.Cast<TEnum>()
.Select(x => new KeyValuePair<TEnum, string>(x, ((Enum)((object)x)).GetDescription()))
.ToList();
}
And finally, for ease of usage, a method to call it directly without value. But then the generic argument is not optional.
public static List<KeyValuePair<TEnum, string>> ToEnumDescriptionsList<TEnum>() {
return ToEnumDescriptionsList<TEnum>(default(TEnum));
}
Now we can use it like this:
enum TestEnum {
[Description("My first value")]
Value1,
Value2,
[Description("Last one")]
Value99
}
var items = default(TestEnum).ToEnumDescriptionsList();
// or: TestEnum.Value1.ToEnumDescriptionsList();
// Alternative: EnumExtensions.ToEnumDescriptionsList<TestEnum>()
foreach (var item in items) {
Console.WriteLine("{0} - {1}", item.Key, item.Value);
}
Console.ReadLine();
Which outputs:
Value1 - My first value
Value2 - Value2
Value99 - Last one
Late to the party, but since there is no accepted answer and it might help others:
As #Maarten mentioned, an extension method works on the value of an enum, not the enum type itelf, so as with Maarteen's soultion you can create a dummy or default value to call the extension method on, however, you may find, as I did, that it is simpler to just use a static helper method like so:
public static class EnumHelper
{
public static SelectList GetSelectList<T>(string selectedValue, bool useNumeric = false)
{
Type enumType = GetBaseType(typeof(T));
if (enumType.IsEnum)
{
var list = new List<SelectListItem>();
// Add empty option
list.Add(new SelectListItem { Value = string.Empty, Text = string.Empty });
foreach (Enum e in Enum.GetValues(enumType))
{
list.Add(new SelectListItem { Value = useNumeric ? Convert.ToInt32(e).ToString() : e.ToString(), Text = e.Description() });
}
return new SelectList(list, "Value", "Text", selectedValue);
}
return null;
}
private static bool IsTypeNullable(Type type)
{
return (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>));
}
private static Type GetBaseType(Type type)
{
return IsTypeNullable(type) ? type.GetGenericArguments()[0] : type;
}
You would create the select list like this:
viewModel.ProvinceSelect = EnumHelper.GetSelectList<Province>(model.Province);
or using the optional numeric values instead of strings:
viewModel.MonthSelect = EnumHelper.GetSelectList<Month>(model.Month,true);
The basic idea for this I got from here, though I changed it to suit my needs. One thing I added was the ability to optionally use ints for the value. I also added an enum extension to get the description attribute which is based on this blog post:
public static class EnumExtensions
{
public static string Description(this Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return en.ToString();
}
}
Since enum can't have extensions pinned to the entire collection a convenient way to extend the Enum base class is with a static typed class. This will allow concise code such as:
Enum<MyCustomEnumType>.GetSelectItems();
Which can be achieved with the following code:
public static class Enum<T>
{
public static SelectListItem[] GetSelectItems()
{
Type type = typeof(T);
return
Enum.GetValues(type)
.Cast<object>()
.Select(v => new SelectListItem() { Value = v.ToString(), Text = Enum.GetName(type, v) })
.ToArray();
}
}
Since enum do not have a shared interface Type misuse is possible, but the class name Enum should dispell any confusion.
Here is a corrected [type casted value to int] and simplified [uses tostring override instead of getname] version of Nathaniels answer that returns a List instead of an array:
public static class Enum<T>
{
//usage: var lst = Enum<myenum>.GetSelectList();
public static List<SelectListItem> GetSelectList()
{
return Enum.GetValues( typeof(T) )
.Cast<object>()
.Select(i => new SelectListItem()
{ Value = ((int)i).ToString()
,Text = i.ToString() })
.ToList();
}
}

Convert an enum to List<string>

How do I convert the following Enum to a List of strings?
[Flags]
public enum DataSourceTypes
{
None = 0,
Grid = 1,
ExcelFile = 2,
ODBC = 4
};
I couldn't find this exact question, this Enum to List is the closest but I specifically want List<string>?
Use Enum's static method, GetNames. It returns a string[], like so:
Enum.GetNames(typeof(DataSourceTypes))
If you want to create a method that does only this for only one type of enum, and also converts that array to a List, you can write something like this:
public List<string> GetDataSourceTypes()
{
return Enum.GetNames(typeof(DataSourceTypes)).ToList();
}
You will need Using System.Linq; at the top of your class to use .ToList()
I want to add another solution:
In my case, I need to use a Enum group in a drop down button list items. So they might have space, i.e. more user friendly descriptions needed:
public enum CancelReasonsEnum
{
[Description("In rush")]
InRush,
[Description("Need more coffee")]
NeedMoreCoffee,
[Description("Call me back in 5 minutes!")]
In5Minutes
}
In a helper class (HelperMethods) I created the following method:
public static List<string> GetListOfDescription<T>() where T : struct
{
Type t = typeof(T);
return !t.IsEnum ? null : Enum.GetValues(t).Cast<Enum>().Select(x => x.GetDescription()).ToList();
}
When you call this helper you will get the list of item descriptions.
List<string> items = HelperMethods.GetListOfDescription<CancelReasonEnum>();
ADDITION:
In any case, if you want to implement this method you need :GetDescription extension for enum. This is what I use.
public static string GetDescription(this Enum value)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name != null)
{
FieldInfo field = type.GetField(name);
if (field != null)
{
DescriptionAttribute attr =Attribute.GetCustomAttribute(field,typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attr != null)
{
return attr.Description;
}
}
}
return null;
/* how to use
MyEnum x = MyEnum.NeedMoreCoffee;
string description = x.GetDescription();
*/
}
In my case, I need to convert it as SelectItem for Checkbox and Radio Button
public class Enum<T> where T : struct, IConvertible
{
public static List<SelectItem> ToSelectItems
{
get
{
if (!typeof(T).IsEnum)
throw new ArgumentException("T must be an enumerated type");
var values = Enum.GetNames(typeof(T));
return values.Select((t, i) => new SelectItem() {Id = i, Name = t}).ToList();
}
}
}

Get a List<string> of my enum attributes with a generic method

At start, we have this basic enum.
public enum E_Levels {
[ValueOfEnum("Low level")]
LOW,
[ValueOfEnum("Normal level")]
NORMAL,
[ValueOfEnum("High level")]
HIGH
}
And I would like to get a List<string> whatever the enum. Something like Extensions.GetValuesOfEnum<E_Levels>() which could return a List<string> with "Low level", "Normal level" and "High level" in it.
StackOF helped me to get one value attribute :
public static class Extensions {
public static string ToValueOfEnum(this Enum value) {
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
ValueOfEnum[] attribs = fieldInfo.GetCustomAttributes(typeof(ValueOfEnum), false) as ValueOfEnum[];
return attribs.Length > 0 ? attribs[0].value : null;
}
}
And I can call this method whatever the enum : E_Levels.LOW.ToValueOfEnum().
Furthermore, StackOF helped me to get a List<string> for a specific enum.
I made this method in a controller :
private List<string> GetLevels() {
List<string> levelsToReturn = new List<string>();
var levels = Enum.GetValues(typeof(E_Levels)).Cast<E_Levels>();
foreach(E_Levels l in levels)
levelsToReturn.Add(l.ToValueOfEnum());
return levelsToReturn;
}
But this way requires me to rewrite the same method for each enum.
So I tried to add this generic method my class Extensions :
public static class Extensions {
public static string ToValueOfEnum(this Enum value) {...}
public static List<string> GetValuesOf<T>() {
List<string> levelsToReturn = new List<string>();
var levels = Enum.GetValues(typeof(T)).Cast<T>();
foreach(T l in levels)
levelsToReturn.Add(l.ToValueOfEnum());
return levelsToReturn;
}
}
But in my foreach, .ToValueOfEnum() is an unknown method.
So I am in trouble, I hoped I could find a way to not rewrite again and again the same method for each enum...
Let's try to keep this more general purpose.
I have an extension method that could grab attributes off of enum values. This would give you quick access to the attributes.
public static class EnumExtensions
{
public static TAttribute GetAttribute<TAttribute>(this Enum value)
where TAttribute : Attribute
{
var type = value.GetType();
var name = Enum.GetName(type, value);
return type.GetField(name)
.GetCustomAttributes(false)
.OfType<TAttribute>()
.SingleOrDefault();
}
}
Using this, you could create some queries to get what you want.
var valuesOfLevels =
Enum.GetValues(typeof(E_Levels)).Cast<E_Levels>()
.Select(level => level.GetAttribute<ValueOfEnumAttribute>().Value);
So your GetValuesOf() method (which is not a great name for such a specialty method IMHO) can be written like this:
public static List<string> GetValuesOf<TEnum>()
where TEnum : struct // can't constrain to enums so closest thing
{
return Enum.GetValues(typeof(TEnum)).Cast<Enum>()
.Select(val => val.GetAttribute<ValueOfEnumAttribute>().Value)
.ToList();
}
Now you may call the method like so:
var levelValues = GetValueOf<E_Levels>();
// levelValues = { "Low level", "Normal level", "High level" }
You might try casting (Enum)(object)l, changing ToValueOfEnum to take an object, or just inline the method:
public static List<string> GetValuesOf<T>()
{
List<string> levelsToReturn = new List<string>();
var levels = Enum.GetValues(typeof(T)).Cast<T>();
foreach (T value in levels)
{
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
ValueOfEnum[] attribs = fieldInfo.GetCustomAttributes(typeof(ValueOfEnum), false) as ValueOfEnum[];
levelsToReturn.Add(attribs.Length > 0 ? attribs[0].value : null);
}
return levelsToReturn;
}
Here's a one-line solution using the casting approach:
return new List<string>(Enum.GetValues(typeof(T)).Cast<Enum>().Select(x => x.ToValueOfEnum()));
In case you weren't clear on why T wasn't recognized as an Enum like E_Levels is, that's because you didn't specify that T : enum. Unfortunately, you can't specify that in C# (even though the CLR supports it), so other approaches like runtime checking/assuming (such as what I'm suggesting here) or post-compile code modifications (e.g. unconstrained-melody) have to be done.
.Net already has the same attribute Description so you can use this one instead ValueOfEnum.
What about dynamic and extension on type and following example
[TestFixture]
public sealed class ForTest
{
[Test]
public void Test()
{
var values = typeof(Levels).ToValues();
values.ForEach(Console.WriteLine);
}
}
public static class TypeExtensions
{
public static List<string> ToValues(this Type value)
{
var result = new List<string>();
var values = ToConcreteValues(value);
foreach (dynamic item in values)
{
Description attribute = GetAttribute<Description>(item);
result.Add(attribute.Description);
}
return result;
}
private static dynamic ToConcreteValues(Type enumType)
{
Array values = Enum.GetValues(enumType);
Type list = typeof (List<>);
Type resultType = list.MakeGenericType(enumType);
dynamic result = Activator.CreateInstance(resultType);
foreach (object value in values)
{
dynamic concreteValue = Enum.Parse(enumType, value.ToString());
result.Add(concreteValue);
}
return result;
}
private static TAttribute GetAttribute<TAttribute>(dynamic value)
where TAttribute : Attribute
{
Type type = value.GetType();
FieldInfo fieldInfo = type.GetField(Enum.GetName(type, value));
return (TAttribute) Attribute.GetCustomAttribute(fieldInfo, typeof (TAttribute));
}
}
public enum Levels
{
[Description("Low level")]LOW,
[Description("Normal level")] NORMAL,
[Description("High level")] HIGH
}
result output:
Low level
Normal level
High level

Categories

Resources