MVC 2 & TypeConverters,获取ConvertFrom中上下文为null的目标类型
我的项目的基类上有一个 TypeConverter 。
[TypeConverter(typeof(CodeTypeConverter))]
abstract class CodeBase
我有许多继承此基类的类,
class TitleCode : CodeBase
class PreferenceCode : CodeBase
当调用类型转换器 (ValueProvider.ConvertSimpleType) 时,它不会创建上下文,因此 ConvertFrom 不会被告知目标类型,因此它可以进行转换。
public override object ConvertFrom(
ITypeDescriptorContext context, // this is null
CultureInfo culture,
object value)
有人遇到过这个问题吗? 如果是这样,你有解决办法吗?
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 type, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们最终做到了这一点;
空上下文检查添加到 CanConvertFrom 方法中,对于上面的情况
它有点错过 MS this,它在我看来,您转换到的类型应该可用。这种解决方法仅在使用我们实现的 ModelBinder 时有效。
啊,好吧,我克服了它
We did this in the end by;
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