Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Building Controls (http://www.velocityreviews.com/forums/f59-asp-net-building-controls.html)
-   -   TypeConverter and Generics (http://www.velocityreviews.com/forums/t758141-typeconverter-and-generics.html)

Kent Boogaart 01-16-2006 10:35 AM

TypeConverter and Generics
 
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




All times are GMT. The time now is 06:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.