Workflow Foundation (WF) — 为什么使用 SetValue() 将 DependencyProperty 设置为 COM 对象会引发 ArgumentException?
假设我有一个 .NET Workflow Foundation (WF) SequenceActivity
类,该类具有以下“输出”属性:
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.Workspace = ...; // exact code not relevant; property set to a COM object
SetValue(WorkspaceProperty, this.Workspace);
最后一行(这使得对 SetValue
的调用)会导致第二个参数出现 ArgumentException
(其值为 this.Workspace
):
依赖属性
Workspace
的类型[…].IWorkspace
与值的类型System.__ComObject
不匹配。
sp; (翻译自德语,英语例外文本可能略有不同)
一旦我使用 typeof(object)
而不是 typeof(IWorkspace)
作为第二个参数注册依赖属性,代码执行得很好。但是,这将导致为依赖属性分配几乎任何值的可能性,而我不希望这样。
在我看来,WF 依赖属性不适用于 COM 互操作对象。
有人对此有解决方案吗?
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
[…].IWorkspace
of dependency propertyWorkspace
does 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了对我自己的问题给出第一个答案,我发现这个解决方法可行:
如果 COM 对象包装在 .NET 对象中,Workflow Foundation 将能够处理它。
(1) 首先定义一个通用帮助器类:
(2) 然后,将上述活动属性定义更改为:
(3) 最后,赋值给
Workspace
属性现在变为:虽然这工作得很好,但它仍然只是一种解决方法。
如果有人有更优雅的解决方案,我很想听听。
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
Workspace
property 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.