Assume that I have a .NET Workflow Foundation (WF) SequenceActivity class with the following “output” property:
public IWorkspace Workspace { get; private set; }
// ^^^^^^^^^^
// important: this is a COM interface type!
public static DependencyProperty WorkspaceProperty = DependencyProperty.Register(
"Workspace",
typeof(IWorkspace),
typeof(FoobarActivity)); // <-- this activity class
This activity executes some code that sets both of the above like this:
this.Workspace = ...; // exact code not relevant; property set to a COM object
SetValue(WorkspaceProperty, this.Workspace);
The last line (which makes the call to SetValue) results in an ArgumentException for the second parameter (having the value of this.Workspace):
Type
[…].IWorkspaceof dependency propertyWorkspacedoes not match the value’s typeSystem.__ComObject.
(translated from German, the English exception text might differ slightly)
As soon as I register the dependency property with typeof(object) instead of typeof(IWorkspace) as the second parameter, the code executes just fine. However, that would result in the possibility to assign just about any value to the dependency property, and I do not want that.
It seems to me that WF dependency properties don’t work for COM interop objects.
Does anyone have a solution to this?
To give a first answer to my own question, I found this work-around to work:
If the COM object is wrapped in a .NET object, Workflow Foundation will be able to deal with it.
(1) First define a generic helper class:
(2) Then, change the above activity property definitions to:
(3) Finally, the assignment to the
Workspaceproperty now becomes:While this works just fine, it’s still only a workaround.
If someone has a more elegant solution, I’d love to hear about it.