PC Review


Reply
Thread Tools Rate Thread

C# Reflection: const string from enum; how to

 
 
Stuart Baker
Guest
Posts: n/a
 
      15th Jul 2004
HI there:

Without delving into too much detail I wish to accomplish
the following:

Given:

enum enumError
{
TooBig,
TooSmall,
Mandatory,
}

const string TooBig = "Oi - this exceeds your limit!";
const string TooSmall = "oooh! Not big enough!";
const string Mandatory = "just got to have it!";

private string GetErrorMessage(enumError err)
{
// what goes here to convert err into
// the required constant?
}

I have found some ways around using reflection but these
are not want I wanted to achieve. For example a hashTable
keyed on (int)err or inedxing into an array... I was just
hoping for a soluition using reflection. I've managed to
convert strings to enum - this one stumps me though

Help would be gratefully received.

Stu
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Jul 2004
Stuart Baker <(E-Mail Removed)> wrote:
> Without delving into too much detail I wish to accomplish
> the following:
>
> Given:
>
> enum enumError
> {
> TooBig,
> TooSmall,
> Mandatory,
> }
>
> const string TooBig = "Oi - this exceeds your limit!";
> const string TooSmall = "oooh! Not big enough!";
> const string Mandatory = "just got to have it!";
>
> private string GetErrorMessage(enumError err)
> {
> // what goes here to convert err into
> // the required constant?
> }
>
> I have found some ways around using reflection but these
> are not want I wanted to achieve. For example a hashTable
> keyed on (int)err or inedxing into an array... I was just
> hoping for a soluition using reflection. I've managed to
> convert strings to enum - this one stumps me though


Here's some sample code:

using System;
using System.Reflection;

enum EnumError
{
TooBig,
TooSmall,
Mandatory,
}

class EnumErrorDescriptions
{
const string TooBig = "Oi - this exceeds your limit!";
const string TooSmall = "oooh! Not big enough!";
const string Mandatory = "just got to have it!";

internal static string GetErrorMessage (EnumError err)
{
string enumName = err.ToString();

FieldInfo fi = typeof(EnumErrorDescriptions).GetField
(enumName, BindingFlags.Static|BindingFlags.NonPublic);
if (fi==null)
{
// Oops! You could throw an exception here if you wanted -
// or return another constant, maybe.
return "Unknown error";
}
return (string)fi.GetValue(null);
}
}

class Test
{
static void Main()
{
Console.WriteLine (EnumErrorDescriptions.GetErrorMessage
(EnumError.TooBig));
}
}

If you'll go through this code path frequently in the same run, you
might want to put the value into a hashtable, only looking it up if you
haven't done so before.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Richard Blewett [DevelopMentor]
Guest
Posts: n/a
 
      12th Sep 2004
I solved this problem using a custom attribute which I applied to the enum entries:

enum enumError
{
[ErrorDesc("Oi - this exceeds your limit!")]
TooBig,
[ErrorDesc("oooh! Not big enough!")]
TooSmall,
...
}

I blogged about it (with the full code) here:

http://staff.develop.com/Richardb/we...View.aspx/.NET

Its near the bottom. I would provide a direct link but there is something not quite right about that one entry as far as my blog engine is concerned:-|

Look for "Extending the humble enum"

The thing I linked about this approach is that the descriptions are bound to the enum rather than being held in some external resource

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

?
nntp://news.microsoft.com/microsoft.public.dotnet.framework/
Stuart Baker wrote:
> Without delving into too much detail I wish to accomplish the
> following:
>
> Given:
>
> enum enumError
> {
> TooBig,
> TooSmall,
> Mandatory,
> }
>
> const string TooBig = "Oi - this exceeds your limit!"; const string
> TooSmall = "oooh! Not big enough!"; const string Mandatory = "just got
> to have it!";
>
> private string GetErrorMessage(enumError err) { // what goes here to
> convert err into // the required constant?
> }
>
> I have found some ways around using reflection but these are not want
> I wanted to achieve. For example a hashTable keyed on (int)err or
> inedxing into an array... I was just hoping for a soluition using
> reflection. I've managed to convert strings to enum - this one stumps
> me though


Here's some sample code:

using System;
using System.Reflection;

enum EnumError
{
TooBig,
TooSmall,
Mandatory,
}

class EnumErrorDescriptions
{
const string TooBig = "Oi - this exceeds your limit!"; const string TooSmall = "oooh! Not big enough!"; const string Mandatory = "just got to have it!";

internal static string GetErrorMessage (EnumError err) { string enumName = err.ToString();

FieldInfo fi = typeof(EnumErrorDescriptions).GetField
(enumName, BindingFlags.Static|BindingFlags.NonPublic);
if (fi==null)
{
// Oops! You could throw an exception here if you wanted - // or return another constant, maybe.
return "Unknown error";
}
return (string)fi.GetValue(null);
}
}

class Test
{
static void Main()
{
Console.WriteLine (EnumErrorDescriptions.GetErrorMessage
(EnumError.TooBig));
}
}

If you'll go through this code path frequently in the same run, you might want to put the value into a hashtable, only looking it up if you haven't done so before.

--
Jon Skeet -
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004

[microsoft.public.dotnet.framework]

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get Const Name and Values with Reflection? Morten Wennevik [C# MVP] Microsoft Dot NET Framework 2 30th Nov 2007 10:32 PM
Modify a const string via reflection/unsafe code? wizofaus@hotmail.com Microsoft C# .NET 2 17th Apr 2007 09:28 AM
Reflection on const variables Stabiplan BV Microsoft VC .NET 1 23rd Mar 2006 03:07 PM
How to get the value of a const using reflection? =?Utf-8?B?TmVvIFRoZSBPbmU=?= Microsoft Dot NET Framework 4 9th Aug 2005 11:06 AM
System::String, std::string and const char* and newline Vasco Lohrenscheit Microsoft VC .NET 0 13th Jan 2004 03:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:28 PM.