自定义IF活动问题 [英] Custom IF Activity issue

查看:46
本文介绍了自定义IF活动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我张贴了我的问题,但仍未得到解答.可能是我无法在其中阐明自己.

I posted my question which is still unanswered. May be I was not able to make myself clear in that.

所以我要再试一次.

我写了一个自定义原生活动,它与"If"非常相似,活动.它必须完成If Activity所做的所有事情,而且还必须具有一些扩展功能.对于扩展功能,我想在运行时基于某些参数创建条件.

I have written an Custom Native Activity which is very similar to "If" Activity. It must do everything which a If Activity does plus it will have some extended feature too. For Extended feature I like to create Condition at runtime base on some parameter.

我在自定义活动中拥有一个名为"CompexCondition"的属性,这将需要字符串,例如([[CustomerId])> 1000英寸并且我将要做的是查询数据库,如果我找到该匹配标准的任何记录,则将条件"设置为条件". 属性为"True"或"1 = 1"在其他情况下,我必须将条件"设置为属性为假"或" 1> 1".在这种情况下,我的然后"和其他"活动将起作用.

I have a property in custom activity called "CompexCondition" which will take string say something like "([CustomerId]) > 1000" and what I will do is that I will query a database and if I will find any record for this matching criteria I will set "Condition" property to "True" or "1 = 1" in other case I have to set "Condition" property to "False" or "1 > 1". And my Then and Else activity will work on this condition.

现在的问题是,由于它是InArgument< bool> ;,所以我无法在运行时更改Condition属性.

Now issue is that I can't change Condition propery at runtime being it a InArgument<bool>.

我可以这样做= SomeFunction(),但是当我使用context.Set或context.Get on Conditon时出现错误.我将Condition更改为InOutArgument< bool>.如果我传递类似"1 = 1"的信息,则会出现编译时错误.作为条件 表达.

I can do like this Condition  = SomeFunction() but when I use context.Set or context.Get on Conditon then I get error. I changed Condition to InOutArgument<bool> then I get compile time error, if I pass something like "1 = 1" as condition expresison.

不确定要得到我想要的东西该做什么.

Not sure what to do in order to get what I want.

有什么建议吗?

 

致谢

 

推荐答案

大家好,

我创建了一个自定义本机活动(与IF活动类似).但这无法执行

I created a custom native activity (which looks simila to IF activity). But this fails in execution saying

不能使用类型为'System.Boolean'的参数.确保在活动中声明了它.

The argument of type 'System.Boolean' cannot be used.  Make sure that it is declared on an activity.

失败是在我试图设置值的位置的Execute方法中.

Failure is in Execute method at the place where I am trying to set value.

请查看以下活动.我想做的是,基于ConditionContainsDatabaseOperation和ComplexExpression属性,我想为InArgument< bool>的值赋值.条件.

Please take a look at the activity below. What I am trying to do is, Based on the ConditionContainsDatabaseOperation and ComplexExpression properties I like to assign the value of InArgument<bool> Condition .


    public sealed class IfActivity : NativeActivity
    {
        public IfActivity()
        {
        }

        public IfActivity(Activity<bool> condition)
            : this()
        {
            if (condition == null)
            {
                throw new System.ArgumentNullException("condition");
            }
            this.Condition = new InArgument<bool>(condition);
        }

        public IfActivity(InArgument<bool> condition) : this()
        {
            if (condition == null)
            {
                throw new System.ArgumentNullException("condition");
            }
            this.Condition = condition;
        }

        public IfActivity(Expression<Func<ActivityContext, bool>> condition) : this()
        {
            if (condition == null)
            {
                throw new System.ArgumentNullException("condition");
            }
            this.Condition = new InArgument<bool>(condition);
        }

        [RequiredArgument]
        [DefaultValue("")]
        public InArgument<bool> Condition { get; set; }

        [DependsOn("Then")]
        [DefaultValue("")]
        public Activity Else { get; set; }

        [DependsOn("Condition")]
        [DefaultValue("")]
        public Activity Then { get; set; }

        [RequiredArgument]
        [DefaultValue(false)]
        public bool ConditionContainsDatabaseOperation { get; set; }

        public List<string> FilterExpressions { get; set; }

        public InOutArgument<DatabaseConnectionResponses> DatabaseConnectionResponses { get; set; }

        public string ComplexCondition { get; set; }

        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            RuntimeArgument argument1 = new RuntimeArgument("Condition", typeof(bool), ArgumentDirection.In);
            metadata.Bind(this.Condition, argument1);
            RuntimeArgument argument2 = new RuntimeArgument("DatabaseConnectionResponses", typeof(DatabaseConnectionResponses), ArgumentDirection.InOut);
            metadata.Bind(this.DatabaseConnectionResponses, argument2);

            Collection<RuntimeArgument> arguments = new Collection<RuntimeArgument>();
            arguments.Add(argument1);
            arguments.Add(argument2);
            metadata.SetArgumentsCollection(arguments);
            Collection<Activity> collection = null;
            if (this.Then != null)
            {
                if (collection == null)
                {
                    collection = new Collection<Activity>();
                }
                collection.Add(this.Then);
            }
            if (this.Else != null)
            {
                if (collection == null)
                {
                    collection = new Collection<Activity>();
                }
                collection.Add(this.Else);
            }
            metadata.SetChildrenCollection(collection);


            this.Condition = new InArgument<bool>(new VisualBasicValue<bool> { ExpressionText = "1 = 1" });
        }

        protected override void Execute(NativeActivityContext context)
        {
            if (ConditionContainsDatabaseOperation)
            {
                context.SetValue(Condition, parseIntoCondition(ComplexCondition, context));
            }

            if (context.GetValue(this.Condition))
            {
                context.ScheduleActivity(this.Then);
            }
            else
            {
                context.ScheduleActivity(this.Else);
            }
        }

        private InArgument<bool> parseIntoCondition(string complexCondition, NativeActivityContext context)
        {
            string newExpression = "1 > 1";
            string columnName = findColumnNameInComplexExpression(complexCondition);
            if (!string.IsNullOrEmpty(columnName))
            {
                DataSet dataSet = getDataSetWhichContainsSpecifiedColumn(columnName, DatabaseConnectionResponses.Get(context));
                if (dataSet != null)
                {
                    newExpression = getNewExpressionAfterApplyingAllTheFilters(dataSet, FilterExpressions);
                }
            }
            return new InArgument<bool>(new VisualBasicValue<bool> { ExpressionText = newExpression });
        }

        private DataSet getDataSetWhichContainsSpecifiedColumn(string columnName, DatabaseConnectionResponses databaseConnectionResponses)
        {
            foreach (DatabaseConnectionResponse databaseConnectionResponse in databaseConnectionResponses.DatabaseConnectionResponseCollection)
            {
                foreach (string columnNameInThisDataSet in databaseConnectionResponse.ColumnNames)
                {
                    if (columnNameInThisDataSet.Equals(columnName))
                        return databaseConnectionResponse.ResultSet;
                }
            }
            return null;
        }

        private string getNewExpressionAfterApplyingAllTheFilters(DataSet dataSet,List<string> FilterExpressions)
        {
            DataSet filteredDataSet = CommonFunctions.FilterDataSetAndReturnFilteredDataSet(dataSet, FilterExpressions);
            if (filteredDataSet.Tables[0].Rows.Count > 0)
                return "1 = 1";
            else
                return "1 > 1";
        }

        private string findColumnNameInComplexExpression(string columnName)
        {
 	        List<string> columnNames = CommonFunctions.FindVariableNameInSuppliedMessage(columnName);
            if(columnNames.Count>0)
            {
                return columnNames[0];
            }
            return string.Empty;
        }
    }


这篇关于自定义IF活动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆