自定义 NativeActivity中泛型类型的限制选项

发布于 2024-12-11 19:49:19 字数 423 浏览 0 评论 0原文

在向工作流添加通用活动时,是否有办法限制和/或更改通用类型选择器向用户呈现的选项?

我们正在将动态工作流支持嵌入到我们的应用程序中,并且我正在构建一组自定义活动来将相关业务对象中的值读取到工作流变量中。因此,现在我们有了 ReadDocumentPropertySetItemProperty 等活动,

这些业务对象包含用户定义的属性集,这些属性集只能是少数基本类型之一(整数、浮点数、字符串、布尔值、日期时间或列表),因此向客户公开整套 .NET 类型以供选择是没有意义的。更糟糕的是,他们最可能想要的是System.Double(用于发票金额等),而这甚至不在默认列表中。

有没有办法更改此提示:替换它、过滤它、预填充默认值等,并且仍然能够使用通用自定义活动?

Is there a way to limit and/or alter the options presented to the user by the generic type picker when adding a generic activity to a workflow?

We are embedding dynamic workflow support into our application, and I'm building a set of custom activities to read values from the associated business objects into workflow variables. So, now we have activities like ReadDocumentProperty<T> and SetItemProperty<T>

These business objects contain user-defined property sets that can only be one of a handful of fundamental types (integer, float, string, boolean, datetime, or list), so it makes no sense to expose the entire set of .NET types to the customer to pick from. Even worse, the one they are most likely to want is System.Double (for invoice amounts, etc) and that's not even in the default list.

Is there any way to change this prompt: replace it, filter it, pre-populate the defaults, etc., and still be able to use a generic custom activity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

安穩 2024-12-18 19:49:19

新的一天,新的想法。我真的忘记这个了。

如果您想在设计时进行验证,完全可以这样做:

public sealed class MyActivity<T> : NativeActivity<T>
    {
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            base.CacheMetadata(metadata);

            if (typeof(T) != typeof(int) &&
                typeof(T) != typeof(bool) &&
                typeof(T) != typeof(DateTime) &&
                typeof(T) != typeof(decimal) &&
                typeof(T) != typeof(string))
            {
                metadata.AddValidationError(
                    "Generic must be 'int', 'DateTime', 'decimal' or 'string'");
            }
        }

        protected override void Execute(NativeActivityContext context)
        {
            // Execution logic 
        }
    }

如果您让客户可以设计自己的工作流程,那么这是完全可以接受的。但请记住,这是设计时验证。如果有人通过代码使用您的活动,它将完美编译,但在运行时验证/执行时会抛出验证异常。


在通用活动上限制类型的唯一方法是使用 where 关键字,就像您使用任何其他 通用类型约束

关于选择通用类型对话框,如果您执行以下操作:

public sealed ReadDocumentProperty<T> : CodeActivity<T>
    where T : struct
{
    //...
}

该对话框将过滤除struct之外的所有类型。也许这样 System.Double 会出现在默认列表中,尽管这只是一个猜测(尚未测试),当然您不能依赖它。此外,如果您第一次选择一种类型,它不在默认列表中,第二次它就会出现在默认列表中。

编辑

据我所知,这个人找到了一种方法“减少“浏览类型”列表”,尽管他使用反思,远非线性、直接或记录的方式。据我所见,它减少了所有编辑器的列表,而不是特定活动的列表(算上您正在使用重新托管的编辑器,否则忘记这一点)

New day, new ideas. I truly forgot this one.

If you want validation at design time, it's totally possible to do something like this:

public sealed class MyActivity<T> : NativeActivity<T>
    {
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            base.CacheMetadata(metadata);

            if (typeof(T) != typeof(int) &&
                typeof(T) != typeof(bool) &&
                typeof(T) != typeof(DateTime) &&
                typeof(T) != typeof(decimal) &&
                typeof(T) != typeof(string))
            {
                metadata.AddValidationError(
                    "Generic must be 'int', 'DateTime', 'decimal' or 'string'");
            }
        }

        protected override void Execute(NativeActivityContext context)
        {
            // Execution logic 
        }
    }

This is perfectly acceptable if you're making activities available for a client to design it's own workflow. But keep in mind that this is design-time validation. If someone uses your activity through code it will compile perfectly but it will thrown a validation exception when validated/executed at runtime.


The only way to constraint a type on a generic activity is with where keyword, just like you were using any other generic type constraint.

Regarding the Select Generic Types dialog, if you do something like this:

public sealed ReadDocumentProperty<T> : CodeActivity<T>
    where T : struct
{
    //...
}

The dialog will filter all types but struct. Maybe that way System.Double appears at the default list, although that's just a guess (haven't test it) and of course you can't rely on it. Moreover, if you select a type, for the first time, that it isn't on the default list, second time it will appear on it.

Edit:

For what I can understand this guy found a way to "reduce the "browse for types" list" although he uses reflection and is nowhere near a linear, direct or documented way of doing it. For what I can see it reduces the list for the all editor and not on a specific activity (counting that you're using the rehosted editor, otherwise forget this)

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