简体   繁体   English

如何检查WCF活动中的属性值?

[英]How to check property value in a WCF activity?

I am really new to windows workflow and I need to create an activity. 我真的是Windows工作流程的新手,我需要创建一个活动。 I did that: 我做到了:

class CustomActivity : Activity { }

This activity has custom a property and I did that: 此活动具有自定义属性,而我做到了:

class CustomActivity : Activity 
{
    /// <summary>
    /// Creation of the Value Property.
    /// </summary>
    [Description("The value of the property to set")]
    [Category("Configuration")]
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string Value { get; set; }
}

Now I would like to check what the user set in this property when he uses the designer. 现在,我想检查用户在使用设计器时在此属性中设置的内容。 For example, if he compiles the workflow, is there any callback when the workflow is compiled, so I could generate compilation error ? 例如,如果他编译工作流程,则在编译工作流程时是否有任何回调,所以我可能会生成编译错误? Or any integrity check callback ? 还是任何完整性检查回调?

Thanks for you help. 感谢您的帮助。

I found the solution by reading WF documentation. 我通过阅读WF文档找到了解决方案。 It is quite easy: 这很容易:

Create a validator object: 创建一个验证器对象:

class CustomActivityValidator : ActivityValidator
{
    public override ValidationErrorCollection ValidateProperties(ValidationManager manager, object obj)
    {
        if (null == manager)
        {
            throw new ArgumentNullException("manager");
        }

        if (null == obj)
        {
            throw new ArgumentNullException("obj");
        }

        CustomActivity activity = obj as CustomActivity;
        if (null == activity)
        {
            throw new ArgumentException("This validator can only be used by the CustomActivity", "obj");
        }

        ValidationErrorCollection errors = base.ValidateProperties(manager, obj);
        if (null != activity.Parent)
        {
            // Now actually validate the activity...
            if (activity.Value != "foobar")
            {
                ValidationError err = new ValidationError("This must be only foobar", 100, false, "Value");
                errors.Add(err);
            }
        }

        return errors;
    }
}

And then you bind this validator to you activity class 然后将这个验证器绑定到您的活动类

[ActivityValidator(typeof(CustomActivityValidator))]
class CustomActivity : Activity
{
    // Your activity code here
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM