Re: Using custom TypeConverter outside of PropertyGrid

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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)
{
}
.



Relevant Pages

  • Re: accessing TypeConverter from code
    ... The PropertyGrid works if the ... TypeConverter is only applied to the Enum, ... Bill ...
    (microsoft.public.dotnet.languages.vb)
  • Re: accessing TypeConverter from code
    ... directly to the Enum. ... applied to a public property in a class, ... PropertyGrid work. ... | I have a TypeConverter for converting between this Enum and Strings. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: accessing TypeConverter from code
    ... So if you put the typeconverter on only the Enum it should work in both ... |> Bill, ... |> | I have a TypeConverter for converting between this Enum and Strings. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Wann wird ein TypeConverter von der BCL benutzt?
    ... Da ich das sicher machen möchte, will ich einen TypeConverter nutzen. ... Wieso nicht die statischen Methoden von Enum? ... Enum.IsDefined stellt sicher, dass die String Werte gültig sind, ... und Enum.Parse kann einen String konvertieren, bzw. ToString ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: accessing TypeConverter from code
    ... To get the converter, be certain to review the parameters to GetConverter... ... | I have a TypeConverter for converting between this Enum and Strings. ... | Public Enum DeviceNameEnum ...
    (microsoft.public.dotnet.languages.vb)