Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Using custom TypeConverter outside of PropertyGrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cartoper - 28 Mar 2008 16:28 GMT
I have written all these wonderful custom TypeConverter for my enums
to display in a PropertyGrid.  I would now like to use the same
TypeConverter to save out and read in the values to and from a text
file, in a generic fashion.  I would like to have a function like
this:

void WriteValue(StreamWriter sw, string name, Enum value);
Cartoper - 28 Mar 2008 18:54 GMT
As I walked to lunch it dawned on me that I might be able to get at
the attributes via the GetType() method.  Well, I wasn't able to, but
I did realize that as long as you follow the convention of <enum
name>TypeConverter, that through reflections you can instanciate the
enum's TypeConverter:

// start by using the default ToString method
string valueStr = Value.ToString();

try
{
    // Get the enum type
    Type enumType = Value.GetType();

    // Get the enum name and then add the TypeConverter to the end of it
    string typeConverterName = enumType.FullName + "TypeConverter";

    // Get the assembly that the enum is in.
    string assemblyName = enumType.Assembly.FullName;

    // try to create the type converter
    ObjectHandle objHandle =
Thread.GetDomain().CreateInstance(assemblyName, typeConverterName);

    // Get the TypeConverter from the object handle
    TypeConverter typeConverter = objHandle.Unwrap() as TypeConverter;

    // Convert it to a string
    valueStr = typeConverter.ConvertToString(Value);
}
catch(Exception)
{
}
Marc Gravell - 28 Mar 2008 19:40 GMT
Or just TypeDescriptor.GetConverter(enumType) ;-p
Marc Gravell - 28 Mar 2008 19:43 GMT
or in fact TypeDescriptor.GetConverter(Value);

I'm also assuming that the enum is marked with TypeConverterAttribute

Marc
Cartoper - 31 Mar 2008 14:20 GMT
> or in fact TypeDescriptor.GetConverter(Value);
>
> I'm also assuming that the enum is marked with TypeConverterAttribute
>
> Marc

Marc,

Where where you Friday, you could have saved me a lot of work;)
Thanks for the tip!
Marc Gravell - 31 Mar 2008 14:31 GMT
No problem; and for the record I replied on Friday!

Marc
Cartoper - 31 Mar 2008 16:01 GMT
> for the record I replied on Friday!

Opps, I guess I ment: where were you while I was at lunch on Firday;)
hehehehehe

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.