TypeConverter and Generics

Discussion in 'ASP .Net' started by Kent Boogaart, Jan 19, 2006.

  1. *tried this already on the buildingcontrols newsgroup but didn't get a
    response*

    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
     
    Kent Boogaart, Jan 19, 2006
    #1
    1. Advertising

  2. Hello Kent,

    You cannot use type parameters in an attribute declaration. It's only logical.
    The attribute is metadata to the class, not part of the class so it cannot
    access class specifics like type parameters or variables and such.

    Although you could do:

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

    But I don't know how much good that would do you.

    --
    Patrik Löwendahl [C# MVP]
    http://www.lowendahl.net

    > *tried this already on the buildingcontrols newsgroup but didn't get a
    > response*
    >
    > 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
     
    =?iso-8859-1?Q?Patrik=20L=f6wendahl=20[C#=20MVP]?=, Jan 19, 2006
    #2
    1. Advertising

  3. Hello Kent,

    You cannot use type parameters in an attribute declaration. It's only logical.
    The attribute is metadata to the class, not part of the class so it cannot
    access class specifics like type parameters or variables and such.

    Although you could do:

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

    But I don't know how much good that would do you.

    --
    Patrik Löwendahl [C# MVP]
    http://www.lowendahl.net

    > *tried this already on the buildingcontrols newsgroup but didn't get a
    > response*
    >
    > 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,
    > Ken
     
    =?iso-8859-1?Q?Patrik=20L=f6wendahl=20[C#=20MVP]?=, Jan 19, 2006
    #3
  4. Hi Patrik,

    Yes I understand why type parameters can't be used in an attribute
    declaration. What I'm wondering is whether there's an accepted way around
    this problem - perhaps a little known API introduced in 2.0? Or do I have to
    resort to placing the TypeConverterAttribute on properties instead of at the
    class level?

    Thanks,
    Kent


    "Patrik Löwendahl [C# MVP]" <> wrote in message
    news:O$...
    > Hello Kent,
    >
    > You cannot use type parameters in an attribute declaration. It's only
    > logical. The attribute is metadata to the class, not part of the class so
    > it cannot access class specifics like type parameters or variables and
    > such.
    >
    > Although you could do:
    > [TypeConverter(typeof(IdConverter<>))] //this will not compile
    > public struct Id<T> where T : IComparable
    > { ... }
    >
    > But I don't know how much good that would do you.
    >
    > --
    > Patrik Löwendahl [C# MVP]
    > http://www.lowendahl.net
    >
    >> *tried this already on the buildingcontrols newsgroup but didn't get a
    >> response*
    >>
    >> 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

    >
    >
     
    Kent Boogaart, Jan 19, 2006
    #4
  5. Kent Boogaart

    Alan Guest

    Alan, Feb 7, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    6,009
    John C. Bollinger
    May 20, 2005
  2. Soul
    Replies:
    0
    Views:
    524
  3. Kent Boogaart

    TypeConverter and Generics

    Kent Boogaart, Jan 16, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    174
    Kent Boogaart
    Jan 16, 2006
  4. Imar Spaanjaars

    TypeConverter for a generics list

    Imar Spaanjaars, Jan 30, 2008, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    752
    Imar Spaanjaars
    Jan 30, 2008
  5. Imar Spaanjaars

    TypeConverter for a StringCollection or a Generics list

    Imar Spaanjaars, Apr 8, 2008, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    933
    Imar Spaanjaars
    Apr 8, 2008
Loading...

Share This Page