Workflow Foundation (WF) — 为什么使用 SetValue() 将 DependencyProperty 设置为 COM 对象会引发 ArgumentException?

发布于 2024-08-29 05:47:14 字数 1313 浏览 2 评论 0原文

假设我有一个 .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 property Workspace does not match the value's type System.__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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○愚か者の日 2024-09-05 05:47:14

为了对我自己的问题给出第一个答案,我发现这个解决方法可行:

如果 COM 对象包装在 .NET 对象中,Workflow Foundation 将能够处理它。


(1) 首先定义一个通用帮助器类:

public class Wrapped<T>
{
    T Value { get; set; }

    public Wrapped(T init)
    {
        Value = init;
    }
}

(2) 然后,将上述活动属性定义更改为:

public Wrapped<IWorkspace> Workspace { get; private set; }
//     ^^^^^^^^^^^^^^^^^^^
//     this is now a .NET class type wrapping a COM object instance.

public static DependencyProperty WorkspaceProperty = DependencyProperty.Register(
        "Workspace",
        typeof(Wrapped<IWorkspace>),
        typeof(FoobarActivity));

(3) 最后,赋值给Workspace 属性现在变为:

this.Workspace = new Wrapped<IWorkspace>(...);

虽然这工作得很好,但它仍然只是一种解决方法。
如果有人有更优雅的解决方案,我很想听听。

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:

public class Wrapped<T>
{
    T Value { get; set; }

    public Wrapped(T init)
    {
        Value = init;
    }
}

(2) Then, change the above activity property definitions to:

public Wrapped<IWorkspace> Workspace { get; private set; }
//     ^^^^^^^^^^^^^^^^^^^
//     this is now a .NET class type wrapping a COM object instance.

public static DependencyProperty WorkspaceProperty = DependencyProperty.Register(
        "Workspace",
        typeof(Wrapped<IWorkspace>),
        typeof(FoobarActivity));

(3) Finally, the assignment to the Workspace property now becomes:

this.Workspace = new Wrapped<IWorkspace>(...);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文