Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How to use a Type Converter to localize enums

I'm trying to understand how to use Type Converters after reading this answer to one of my other questions. But I'm not sure if I quite get it...

In my particular case I would like to "convert" an enum member into a localized string by getting a resource string depending on what enum member it is. So for example if I had this enum:

public enum Severity
{
    Critical,
    High,
    Medium,
    Low
}

or this:

public enum Color
{
    Black = 0x0,
    Red = 0x1,
    Green = 0x2,
    Blue = 0x4,
    Cyan = Green | Blue,
    Magenta = Red | Blue,
    Yellow = Red | Green,
    White = Red | Green | Blue,
}

How would I create a Type Converter that could convert those members into localized strings? And how would I use it? Currently I would need to use it in a WinForms application, but more general examples are welcome as well.

like image 992
Svish Avatar asked Jul 13 '09 06:07

Svish


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

To create a TypeConverter, simply create a class that inherits from TypeConverter. Then you use the TypeConverterAttribute to tag your class, so that anytime someone tries a convert operation on your class, your TypeConverter is invoked.

Once you inherit from TypeConverter, you should override some of its methods to do what you want. You'd probably want to look at ConvertFrom(), ConvertTo(), and ConvertToString() to start with - that's where you would implement the logic to pull out your localized version of your strings.

To use your TypeConverter, you would code something like this:

var foo = TypeDescriptor.GetConverter(typeof(T));
var mystring = foo.ConvertToString(myObject));

MSDN of course has the documentation and some examples of TypeConverter implementation.

like image 130
womp Avatar answered Sep 19 '22 15:09

womp


I believe this was already answered in How do I override ToString in C# enums?

Also, you could combine this with an extension method for enums with a name like ToDisplayString.

like image 23
Steven Sudit Avatar answered Sep 18 '22 15:09

Steven Sudit