我可能会离开,或者真的很接近。无论哪种方式,我目前都是 SOL。:)
我希望能够使用扩展方法来设置类的属性,但是该类可能(或可能不会)在非 UI 线程上更新,并且派生自强制更新在 UI 线程上的类(它实现了 INotifyPropertyChanged 等)。
我有一个类定义是这样的:
public class ClassToUpdate : UIObservableItem
{
private readonly Dispatcher mDispatcher = Dispatcher.CurrentDispatcher;
private Boolean mPropertyToUpdate = false;
public ClassToUpdate() : base()
{
}
public Dispatcher Dispatcher
{
get { return mDispatcher; }
}
public Boolean PropertyToUpdate
{
get { return mPropertyToUpdate; }
set { SetValue("PropertyToUpdate", ref mPropertyToUpdate, value; }
}
}
我有一个扩展方法类定义如下:
static class ExtensionMethods
{
public static IEnumerable<T> SetMyProperty<T>(this IEnumerable<T> sourceList,
Boolean newValue)
{
ClassToUpdate firstClass = sourceList.FirstOrDefault() as ClassToUpdate;
if (firstClass.Dispatcher.Thread.ManagedThreadId !=
System.Threading.Thread.CurrentThread.ManagedThreadId)
{
// WHAT GOES HERE?
}
else
{
foreach (var classToUpdate in sourceList)
{
(classToUpdate as ClassToUpdate ).PropertyToUpdate = newValue;
yield return classToUpdate;
}
}
}
}
显然,我正在扩展方法中寻找“这里有什么”。
谢谢,wTS