开发者

MVC 2 & TypeConverters, Get the destination type in ConvertFrom where the context is null

开发者 https://www.devze.com 2023-01-21 05:54 出处:网络
I have a TypeConverter on the base class in my project. [TypeConverter(typeof(CodeTypeConverter))] abstract class CodeBase

I have a TypeConverter on the base class in my project.

[TypeConverter(typeof(CodeTypeConverter))]
abstract class CodeBase

I have a number of classes which inherit this base

class TitleCode : CodeBase
class PreferenceCode : CodeBase

When the thing that calls the type converter (ValueProvider.ConvertSimpleType) it does not create a context and so ConvertFrom is not informed of the destination ty开发者_JS百科pe, so it can do the conversion.

public override object ConvertFrom(
  ITypeDescriptorContext context, // this is null
  CultureInfo culture,
  object value)

Has anyone come across this problem? And, if so, do you have a work around?


We did this in the end by;

  1. getting the TypeConverter for the object
  2. creating a proxy context class implementing ITypeDescriptorContext
  3. creating a property descriptor with the required type set

a check for null context was add to the CanConvertFrom method, for cases such as above

Its a bit of a miss from MS this, it seems to me that the type your converting to should be available. This work around only works when the ModelBinder we implemented is being used.

Ah well, I got over it

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号