Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > TypeConverter and Generics

Reply
Thread Tools

TypeConverter and Generics

 
 
Kent Boogaart
Guest
Posts: n/a
 
      01-16-2006
Hi,

As far as I can tell, it is not possible to use the TypeConverter
infrastructure with generic types. Say you have this type:

public struct Id<T> where T : IComparable
{ ... }

And you want to convert from strings to Id<T> and vice-versa. So you write a
generic TypeConverter:

public IdConverter<T> : TypeConverter
{ ... }

But then you'll find you can't apply the generic type converter:

[TypeConverter(typeof(IdConverter<T>))] //this will not compile
public struct Id<T> where T : IComparable
{ ... }

You can attempt to hack together a solution based on the
ITypeDescriptorContext parameter to the various TypeConverter methods.
However, this is unreliable since this parameter isn't always passed in (and
certainly wasn't by VS.NET when I tried it). As far as I can tell, the
possible workarounds are:
1. Implement a generic type converter and apply it manually to each Id<T>
property instead of applying it only once to the Id<T> struct.
2. Forgo generics and use subclasses, each with their own TypeConverter
applied.
3. Don't use type converters.

Can anyone tell me whether I'm missing something?

Thanks,
Kent


 
Reply With Quote
 
 
 
Reply

Thread Tools

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
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
TypeConverter for a StringCollection or a Generics list Imar Spaanjaars ASP .Net Building Controls 0 04-08-2008 07:20 AM
TypeConverter for a generics list Imar Spaanjaars ASP .Net Building Controls 0 01-30-2008 07:52 PM
TypeConverter and Generics Kent Boogaart ASP .Net 4 02-07-2006 09:46 PM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57