1

我有一段 C++/CLI 代码,它使用反射 API 查询某些 WPF 控件的属性值,如下所示:

Type ^t = ...;
Object ^o = ...;
PropertyInfo ^p = t->GetProperty( "Enabled" );
Object ^v = p->GetValue( o, nullptr );

这工作得很好,但现在我不得不将此代码移动到一个单独的线程。这样做会使最后一次GetValue调用产生关于从被禁止的不同线程访问对象的异常。

知道我的 WPF 控件是 DispatcherObjects,我知道我可以在它们上使用 Invoke() 并传递一个 Action 让一段代码在与目标对象相同的线程中执行。但是,我不知道如何在 C++/CLI 中执行此操作。特别是,我怎样才能传递一个函数(即接受参数并返回一个值的东西)?

理想情况下,我可以做类似的事情

// Toplevel code:
delegate Object ^GetPropertyDelegate( Object ^o, PropertyInfo ^p );

// Then, at the place where I perform the GetValue() call:
struct Local {
    static Object ^run( Object ^o, PropertyInfo ^p ) {
        return p->GetValue( o, nullptr );
    }
};

Type ^t = ...;
Object ^o = ...;
PropertyInfo ^p = t->GetProperty( "Enabled" );
DispatcherObject ^dispObj = dynamic_cast<DispatcherObject ^>( o );
Object ^v = dispObj->Dispatcher->Invoke( gcnew GetPropertyDelegate( &Local::run ) );

从技术上讲,这可以编译 - 但它没有意义。理想情况下,我希望在 'o' 和 'p' 上有一个轻量级(即不需要太多代码)闭包,作为我可以传递给 Dispatcher::Invoke 的东西。有人知道怎么做这个吗?

4

1 回答 1

1

像下面这样的东西应该可以工作。它使用Func<T1, T1, TResult>委托来调用静态方法。方法参数被传递给Dispatcher.Invoke调用。

public ref class YourClass
{
private:
    static Object^ GetValue(Object^ queryObject, PropertyInfo^ queryProperty)
    {
        return queryProperty->GetValue(queryObject);
    }

public:
    static Object^ GetPropertyValue(
        DispatcherObject^ dispObj, PropertyInfo^ propertyInfo)
    {
        return dispObj->Dispatcher->Invoke(
            gcnew Func<Object^, PropertyInfo^, Object^>(&YourClass::GetValue),
            dispObj, propertyInfo);
    }
};

以下代码甚至根本不需要静态方法。Func<Object^, Object^>它直接从PropertyInfo实例和PropertyInfo::GetValue方法创建一个委托。不知道它是否是有效的 C++/CLI,但它对我来说很好用。

Object^ result = dispObj->Dispatcher->Invoke(
    gcnew Func<Object^, Object^>(propertyInfo, &PropertyInfo::GetValue), dispObj);
于 2013-07-16T13:33:48.563 回答