我可能完全错了,也可能非常接近。无论如何,我现在很无助。:)
我想使用扩展方法来设置一个类的属性,但这个类可能(也可能不)在非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;
}
}
}
}
显然,我正在寻找扩展方法中的“WHAT GOES HERE”。
谢谢, wTs
sourceList.SetMyProperty(true).SetMyOtherProperty("hello").SetFoo(42)...
- Thomas Levesque