internal class ShouldLimitNameLengthConverter : IValueConverter
{
const int mIdealNameLengthMax = 20;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string)
return ((string)value).Length > mIdealNameLengthMax;
throw (new NotSupportedException());
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
Debug.Assert(false);
throw (new NotSupportedException());
}
}在改一个用户发来的crash report,通过windbg调试dump文件,确认了crash发生在Convert函数,异常是NotSupportedException,那么原因很简单,就是value is string返回了true。这个converter是WPF的DataTemplate调用的,绑定是一个对象的Text属性,是String类型。照理说没问题啊!百思不得其解!终于查了MSDN发现,in关键字是会check null的,只要对象为空就返回false:
An is expression evaluates totrue if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.
呵呵,长知识了!
本文详细解析了一个在WPF项目中遇到的C#内部类使用问题,通过Windbg调试dump文件,最终揭示了由null检查引起的异常。文章提供了深入的解释,帮助开发者理解内部类的特性及其在实际应用中的注意事项。
1575

被折叠的 条评论
为什么被折叠?



