这里是我的扩展方法:的WPF控件的extenstion方法引起StackOverflowException
public static void SetThreadSafeProperty<T>(this System.Windows.FrameworkElement control, Expression<Func<T>> property, T value)
{
if (control.Dispatcher.CheckAccess())
{
var del = new SetThreadSafePropertyDelegate<T>(SetThreadSafeProperty);
control.Dispatcher.Invoke(del, control, property, value);
}
else
{
PropertyInfo propertyInfo = GetPropertyInfo(property);
if (propertyInfo != null)
{
propertyInfo.SetValue(control, value, null);
}
}
}
而这里的如何,我叫它:
tbManufacturer.SetThreadSafeProperty(() => tbManufacturer.Text, "test");
调试它之后,它看起来像它陷入无限循环。 CheckAcess()是真的,它创建的deletegate只是罚款和调用正确。但它不断前进并最终失败。
关于这可能发生的原因的任何想法?
与调试器断点运行它,看看问题变得明显。 – 2014-11-20 19:36:52
它应该是相反的:'if(!control.Dispatcher.CheckAccess())' – Clemens 2014-11-20 19:37:24
@Clemens就是这样,谢谢! – ernest 2014-11-20 19:42:07