Enum and generics
-
PIEBALDconsult wrote:
Does no one read my articles?
Sorry, I just missed it.
PIEBALDconsult wrote:
I suspect that all that checking for null in your example is needless.
I am not getting you fully. Are you saying that the methods I used to get "FieldInfo", attributes will never return NULL ?
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
I have a generic method which looks for a specific attribute and returns value of "Text" property. My attribute is named "DetailsAttribute". Here is my generic method
public static string GetDescription<EnumType>(EnumType enumType) {
string description = string.Empty; Type type = enumType.GetType(); if (type != null) { // Getting filed info FieldInfo info = type.GetField(enumType.ToString()); if (info != null) { // getting the attributes DetailsAttribute\[\] attributes = info.GetCustomAttributes(typeof(DetailsAttribute), false) as DetailsAttribute\[\]; if (attributes != null && attributes.Length > 0) description = attributes\[0\].Text; } } return description;
}
This works fine. But I am looking for applying a constraint to the generic parameter "enumType" which should allow only enum types. I am not able to write something like
public static string GetDescription<EnumType>(EnumType enumType) : where enumType : enum // error
How can I set such kind of restriction ? Also is there any better method than what I provided to retrieve attribute values from fields ? any help would be appreciated.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
You can't apply a generic constraint on an enum type. The best you can get is struct. Also, take a look at this article[^] for a way to work with enums and a description attribute.
Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
-
PIEBALDconsult wrote:
Does no one read my articles?
Hmmm...I missed that one somehow. Interesting approach to things. Did you see my article[^]? I have a similar GetDescription method.
Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
-
PIEBALDconsult wrote:
Does no one read my articles?
Sorry, I just missed it.
PIEBALDconsult wrote:
I suspect that all that checking for null in your example is needless.
I am not getting you fully. Are you saying that the methods I used to get "FieldInfo", attributes will never return NULL ?
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Correct. GetType() won't and GetCustomAttributes won't, GetField won't because you're passing in a known field name (in this case).
modified on Tuesday, July 8, 2008 12:41 AM
-
PIEBALDconsult wrote:
Does no one read my articles?
Hmmm...I missed that one somehow. Interesting approach to things. Did you see my article[^]? I have a similar GetDescription method.
Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
I probably did, I definitely remember the picture of someone blocking the Green Monster.
-
I have a generic method which looks for a specific attribute and returns value of "Text" property. My attribute is named "DetailsAttribute". Here is my generic method
public static string GetDescription<EnumType>(EnumType enumType) {
string description = string.Empty; Type type = enumType.GetType(); if (type != null) { // Getting filed info FieldInfo info = type.GetField(enumType.ToString()); if (info != null) { // getting the attributes DetailsAttribute\[\] attributes = info.GetCustomAttributes(typeof(DetailsAttribute), false) as DetailsAttribute\[\]; if (attributes != null && attributes.Length > 0) description = attributes\[0\].Text; } } return description;
}
This works fine. But I am looking for applying a constraint to the generic parameter "enumType" which should allow only enum types. I am not able to write something like
public static string GetDescription<EnumType>(EnumType enumType) : where enumType : enum // error
How can I set such kind of restriction ? Also is there any better method than what I provided to retrieve attribute values from fields ? any help would be appreciated.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Oh, and... a few months ago I asked about it on MSDN and Jon Skeet said he'd ask whether or not an enum constraint might be in the future, he later reported... "maybe". I suspect there are a lot more important things concerning them. I think maybe we should start a letter campaign.
-
Correct. GetType() won't and GetCustomAttributes won't, GetField won't because you're passing in a known field name (in this case).
modified on Tuesday, July 8, 2008 12:41 AM
Thanks. I will remove the NULL checking. Thanks
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
You can't apply a generic constraint on an enum type. The best you can get is struct. Also, take a look at this article[^] for a way to work with enums and a description attribute.
Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
Scott, Thanks. I figured it out. Great article though
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Oh, and... a few months ago I asked about it on MSDN and Jon Skeet said he'd ask whether or not an enum constraint might be in the future, he later reported... "maybe". I suspect there are a lot more important things concerning them. I think maybe we should start a letter campaign.
PIEBALDconsult wrote:
I asked about it on MSDN and Jon Skeet said he'd ask whether or not an enum constraint might be in the future,
Ohh, so what could replace enums ? Jon Skeet - I love that guy. He got indepth knowledge on the subject and very helping too. He has a book out "C# in Depth". I got a copy, it's worth reading.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Scott, Thanks. I figured it out. Great article though
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
N a v a n e e t h wrote:
Thanks. I figured it out. Great article though
You're welcome. Glad you liked the article.
Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
-
PIEBALDconsult wrote:
I asked about it on MSDN and Jon Skeet said he'd ask whether or not an enum constraint might be in the future,
Ohh, so what could replace enums ? Jon Skeet - I love that guy. He got indepth knowledge on the subject and very helping too. He has a book out "C# in Depth". I got a copy, it's worth reading.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
I meant it's possible that a future C# compiler will allow
where T : enum
I'm not holding my breath, but apparently the team who can make it happen know there's some demand for it. -
I meant it's possible that a future C# compiler will allow
where T : enum
I'm not holding my breath, but apparently the team who can make it happen know there's some demand for it.PIEBALDconsult wrote:
I meant it's possible that a future C# compiler will allow where T : enum
I misunderstood you, now it's clear.
PIEBALDconsult wrote:
know there's some demand for it.
Yeah. Let's hope it would come in the future versions.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions