C# Reflection: const string from enum; how to

  • Thread starter Thread starter Stuart Baker
  • Start date Start date
S

Stuart Baker

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
 
Stuart Baker said:
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.
 
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/weblog/CategoryView.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 said:
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]
 
Back
Top