|
-
Mar 12th, 2012, 06:58 PM
#1
Thread Starter
PowerPoster
Generic value types
im trying to make a generic function which converts an enum into a dictionary.
The dictionary however I want to return would be:
Dictionary<TValue, string>
where TValue could be an int, byte, long etc...
How can I do this?
this is my current method signature:
Code:
public static IDictionary<int, string> ToDictionary<TEnum>() where TEnum : struct
-
Mar 12th, 2012, 08:50 PM
#2
Re: Generic value types
Code:
public static IDictionary<T, string> ToDictionary<T>() where T : struct
-
Mar 12th, 2012, 09:41 PM
#3
Re: Generic value types
I'm not sure that that really helps you because there are a lot of structures that are not enumerations. I'd say that, even with that, you would need to call Type.IsEnum within your method and throw an exception if it's false.
-
Mar 13th, 2012, 12:17 AM
#4
Re: Generic value types
Answered on SO:
http://stackoverflow.com/questions/7...g-t-to-an-enum
Two main approaches in there: 1) Get as close to the constraint as you can and then throw an exception as jmc suggests
2) Write it in IL! This then becomes more interesting from a build point of view! (I like this one lots! Although that might be because I won't actually have to deal with it and it's just a theoretical exercise for me! )
-
Mar 13th, 2012, 02:46 AM
#5
Thread Starter
PowerPoster
Re: Generic value types
thats ok in regards to checking if it is an enum and throwing an exception, I have that in place.
I'm just trying to get the TValue type generic but not sure how. will check the SO link 
in the solution, the enum maybe an int, byte etc... so I want to be able to convert an enum to a dictionary so its <string, TValue> - just not sure how to do the TValue part (not worked with generics much!)
Last edited by Techno; Mar 13th, 2012 at 02:51 AM.
-
Mar 13th, 2012, 06:46 PM
#6
Re: Generic value types
That's not how generics work. Sounds like you want something like:
csharp Code:
public static IDictionary<TUnderlying, string> ToDictionary<TEnum>() where TEnum : Enum<TUnderlying>
Unfortunately, such a syntax does not exist. Your calling code will be the one to 'close' the generic definition, so it will need to know what the underlying type is before it could specify it (and it needs specifying as you can't infer it from the type of the Enum). Therefore, as it only affects the return value, using generics is a bad fit here, IMO. I would have different methods:
csharp Code:
public static IDictionary<int, string> ToIntDictionary<TEnum>()... public static IDictionary<byte, string> ToByteDictionary<TEnum>()... public static IDictionary<long, string> ToLongDictionary<TEnum>()...
-
Mar 13th, 2012, 06:48 PM
#7
Thread Starter
PowerPoster
Re: Generic value types
Thanks.
i know what Generics are I guess I was using it in the wrong context than what I meant.
I was trying to avoid the underlaying type issue and not have to resort to creating x overloads with "strongly/concrete type" for the underlaying type (i.e int, byte, string etc...)
hmph.
-
Mar 13th, 2012, 06:53 PM
#8
Re: Generic value types
I know how you feel. I used to think I could avoid some of that with Generics, but when you think about it, in this scenario of trying to get Generic output, what is the difference between this:
csharp Code:
int integerResult = GetAs<Integer>(); byte byteResult = GetAs<Byte>(); long longResult = GetAs<Long>();
and this:
csharp Code:
int integerResult = GetAsInteger(); byte byteResult = GetAsByte(); long longResult = GetAsLong();
You don't gain anything with Generics - you still have effectively three different methods. What you do get, however, is to make the implementation more complex (if it's even possible).
-
Mar 13th, 2012, 07:08 PM
#9
Thread Starter
PowerPoster
Re: Generic value types
My argument is that the benefit is... less code....
you create 1 method... and return the result using the developer defined "T" if that makes sense
-
Mar 13th, 2012, 08:57 PM
#10
Re: Generic value types
Looking back at your original question, it appears that there's no actual value to this Dictionary anyway. It appears that you want a Dictionary that will allow you to provide an enum value and give you back a string. Doesn't the ToString method of the enum value already do that? If you want a more descriptive string then you might want to check out CodeBank thread on Friendly Enumeration Names.
-
Mar 14th, 2012, 02:30 AM
#11
Thread Starter
PowerPoster
Re: Generic value types
Thanks.
actually, thats not quite what I want. What I want is a way to bind to a dropdownlist with a key and value and no dups and that enum datasource could be from any type of enum with any type of underlaying value
-
Mar 14th, 2012, 03:21 AM
#12
Re: Generic value types
 Originally Posted by Techno
Thanks.
actually, thats not quite what I want. What I want is a way to bind to a dropdownlist with a key and value and no dups and that enum datasource could be from any type of enum with any type of underlaying value 
Then I still don't see that a Dictionary is the way to go.
csharp Code:
myDropDownList.DataSource = Enum.GetValues(typeof(SomeEnum)).Select(e => new {.Text = e.ToString(), .Value = e});
-
Mar 14th, 2012, 04:51 AM
#13
Thread Starter
PowerPoster
Re: Generic value types
Thanks.
well thats the short version of what I want. its just to do with a legacy type system so im trying to improve the code from my end to be "better" and more generic/flexible. it may not need to bind to a dropdownlist, could be something else non UI related
I'd still like the solution with a Dictionary
-
Mar 16th, 2012, 02:38 PM
#14
Thread Starter
PowerPoster
Re: Generic value types
here is the solution:
Code:
public static class EnumCollectionHelper
{
public static IDictionary<Val, string> ToDictionary<T, Val>()
{
Val[] values = Enum.GetValues(typeof(T)) as Val[];
return values.Zip(Enum.GetNames(typeof(T)), (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v);
}
}
usage:
Code:
// get me an enum dictionary with the value being of type byte, as that is what my "EnumHere" is.
IDictionary myDictionary = EnumCollectionHelper.ToDictionary<EnumHere, byte>();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|