Take the 2-minute tour ×

I have a piece of C++/CLI code which queries the value of a property of some WPF control using the reflection API, like this:

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

This worked well, but now I had to move this code to a separate thread. Doing so made the last GetValue invocation yield exceptions about accessing the object from a different thread being forbidden.

Knowing that my WPF controls are DispatcherObjects, I know I can use Invoke() on them and pass an Action to have a piece of code executed in the same thread as the target object. However, I don't know how to do this in C++/CLI. In particular, how can I pass a Function (i.e. something which takes arguments and returns a value)?

Ideally, I could do something like

// 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 = ...;
DispatcherObject ^dispObj = dynamic_cast<DispatcherObject ^>( o );
Object ^v = dispObj->Dispatcher->Invoke( gcnew GetPropertyDelegate( &Local::run ) );

Technically, this compiles - but it doesn't make sense. Ideally, I'd like to have a lightweight (i.e., doesn't require too much code) closure over 'o' and 'p' as something which I can pass to Dispatcher::Invoke. Does anybody know how to do this?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Something like the following should work. It uses a Func<T1, T1, TResult> delegate to call a static method. The method arguments are passed to the Dispatcher.Invoke call.

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

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

The following piece of code does not even require a static method at all. It directly creates a Func<Object^, Object^> delegate from the PropertyInfo instance and the PropertyInfo::GetValue method. Don't know if it's valid C++/CLI, but it works fine for me.

Object^ result = dispObj->Dispatcher->Invoke(
    gcnew Func<Object^, Object^>(propertyInfo, &PropertyInfo::GetValue), dispObj);
share|improve this answer
+1: Ah, interesting - I wasn't aware of Func. I'm trying to digest the documentation of what arguments the Func constructor takes. Can it only be used to create delegates for objects methods, i.e. not static (class) methods? I tried to adapt your code so that it uses a plain static function Object ^GetValue( Object ^o, PropertyInfo ^p ); and then pass a gcnew Func<Object ^, PropertyInfo ^, Object ^>( dispObj, propertyInfo, &GetValue ); to Invoke, hoping that I could easily closer over dispObj and propertyInfo without having a dedicated class... –  Frerich Raabe Jul 16 '13 at 14:08
See the edit. Maybe that helps... –  Clemens Jul 16 '13 at 14:37
@JochenKalmbach And that's exactly what was asked for. GetValue has to be called in the UI thread in order to avoid the TargetInvocationException mentioned in the question. –  Clemens Jul 16 '13 at 16:42
Clemens, you last edit which doesn't even require a static method is pretty sweet! :-) I can't imagine it'll get any better than that, so I'll accept your answer. –  Frerich Raabe Jul 17 '13 at 6:43

Your Answer


