title: How do I query a WPF property from a different thread in C++/CLI?
body: <p>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:</p>
  
  <pre><code>Type ^t = ...;
  Object ^o = ...;
  PropertyInfo ^p = t-&gt;GetProperty( "Enabled" );
  Object ^v = p-&gt;GetValue( o, nullptr );
  </code></pre>
  
  <p>This worked well, but now I had to move this code to a separate thread. Doing so made the last <code>GetValue</code> invocation yield exceptions about accessing the object from a different thread being forbidden.</p>
  
  <p>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)?</p>
  
  <p>Ideally, I could do something like</p>
  
  <pre><code>// 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-&gt;GetValue( o, nullptr );
      }
  };
  
  Type ^t = ...;
  Object ^o = ...;
  PropertyInfo ^p = t-&gt;GetProperty( "Enabled" );
  DispatcherObject ^dispObj = dynamic_cast&lt;DispatcherObject ^&gt;( o );
  Object ^v = dispObj-&gt;Dispatcher-&gt;Invoke( gcnew GetPropertyDelegate( &amp;Local::run ) );
  </code></pre>
  
  <p>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?</p>