Saturday, 15 May 2010

c# - Cannot create a TypeConverter for a generic type -



c# - Cannot create a TypeConverter for a generic type -

i'd create typeconverter generic class, this:

[typeconverter(typeof(wrapperconverter<t>))] public class wrapper<t> { public t value { // & set } // other methods } public class wrapperconverter<t> : typeconverter<t> { // back upwards , strings public override bool canconvertfrom(itypedescriptorcontext context, type sourcetype) { if (sourcetype == typeof(string)) { homecoming true; } homecoming base.canconvertfrom(context, sourcetype); } public override bool canconvertto(itypedescriptorcontext context, type destinationtype) { if (destinationtype == typeof(string)) { homecoming true; } homecoming base.canconvertto(context, destinationtype); } public override object convertfrom(itypedescriptorcontext context, system.globalization.cultureinfo culture, object value) { if (value string) { typeconverter converter = typedescriptor.getconverter(typeof(t)); t inner = converter.convertto(value, destinationtype); homecoming new wrapper<t>(inner); } homecoming base.convertfrom(context, culture, value); } public override object convertto(itypedescriptorcontext context, system.globalization.cultureinfo culture, object value, type destinationtype) { if (destinationtype == typeof(system.string)) { wrapper<t> wrapper = value wrapper<t>(); typeconverter converter = typedescriptor.getconverter(typeof(t)); homecoming converter.convertto(wrapper.value, destinationtype); } homecoming base.convertto(context, culture, value, destinationtype); } }

the problem comes in cannot have generic in line, disallowed:

[typeconverter(typeof(wrapperconverter<t>))] public class wrapper<t>

my next approach seek define single, non-generic converter handle wrapper<t> instances. mix of both reflection , generics has me stumped on how implement both convertto , convertfrom methods.

so example, convertto looks this:

public override object convertto(itypedescriptorcontext context, system.globalization.cultureinfo culture, object value, type destinationtype) { if (destinationtype == typeof(system.string) && value.gettype().isgenerictype) { // 1. how enforce value wrapper<t> instance? type innertype = value.gettype().getgenericarguments()[0]; typeconverter converter = typedescriptor.getconverter(innertype); // 2. how t value property? introduce interface wrapper<t> implements maybe? object innervalue = ??? homecoming converter.convertto(innervalue, destinationtype); } homecoming base.convertto(context, culture, value, destinationtype); }

in convertfrom have biggest problem because have no way know wrapper class convert incomming strings into.

i've created several custome types , typeconverters utilize asp.net 4 web api framework, , need used well.

one other thing tried assign generic version of converter @ runtime seen here, webapi framework did not respect (meaning, converter never created).

one lastly note, i'm using .net 4.0 , vs 2010.

i solved creating single converter hanlde of types derrived generic class. big issue of knowing generic arg t within convertfrom solved capturing info in constructor seen below.

public mygenericconverter(type type) { if (type.isgenerictype && type.getgenerictypedefinition() == typeof(mygenericclass<>) && type.getgenericarguments().length == 1) { _genericinstancetype = type; _innertype = type.getgenericarguments()[0]; _innertypeconverter = typedescriptor.getconverter(_innertype); } else { throw new argumentexception("incompatible type", "type"); } }

it took me ages find .net infrastructure reflectively calls constructor overload if defined. not part of documented typeconverter class.

hope helps next guy.

c# visual-studio-2010 asp.net-web-api asp.net-4.0 typeconverter

No comments:

Post a Comment