Windows工作流基础 - 在工作流中的不同活动之间传递自定义变量

时间:2013-03-14 20:48:52

标签: c#

我将自定义变量类型从一个操作传递到工作流中的另一个操作。以下是自定义对象的定义

       public class ConfigDatabase
        {
            public string Name;
            public string Host;
            public string Port;
            public string Instance;
            public string User;
            public string Password;
        }

        public class ConfigDatabases
        {
            public string DatabaseToUse;
            public List<ConfigDatabase> DatabaseList;

            public ConfigDatabases()
            {
                DatabaseList = new List<ConfigDatabase>();
            }
        }

        public class ConfigEnvironment
        {
            public ConfigDatabases EnvironmentConfigDatabase;

            public ConfigEnvironment()
            {
                EnvironmentConfigDatabase = new ConfigDatabases();
            }

            public ConfigDatabase ReturnDatabaseInfo()
            {
                ConfigDatabase ConfigDatabaseInfo = new ConfigDatabase();
                for (int Count1 = 0; Count1 < EnvironmentConfigDatabase.DatabaseList.Count; Count1++)
                {
                    if (EnvironmentConfigDatabase.DatabaseList[Count1].Name == EnvironmentConfigDatabase.DatabaseToUse)
                    {
                        ConfigDatabaseInfo = EnvironmentConfigDatabase.DatabaseList[Count1];
                        return ConfigDatabaseInfo;
                    }
                }
                return ConfigDatabaseInfo;
            }

            public string GetDatabaseConnectionString()
            {
                ConfigDatabase DatabaseInfo = ReturnDatabaseInfo();
                string ConnectionString = "Data Source=(description=(address=(protocol=tcp)(host=" + DatabaseInfo.Host + ")(port=" + DatabaseInfo.Port + "))(connect_data=(sid=" + DatabaseInfo.Instance + ")));User ID=" + DatabaseInfo.User + ";Password=" + DatabaseInfo.Password + ";";
                return ConnectionString;
            }
        }

在操作的第一步中,它将运行以下代码从文件加载配置数据并存储在函数Exec​​ute中返回的对象(ConfigEnvironment)

public sealed class InitializeEnvironment : CodeActivity<ConfigEnvironment>
{
    // Define an activity input argument of type string
    public InArgument<string> EnvironmentFileLocation { get; set; }

    // If your activity returns a value, derive from CodeActivity<TResult>
    // and return the value from the Execute method.
    protected override ConfigEnvironment Execute(CodeActivityContext context)
    {
        // Obtain the runtime value of the Text input argument
        string EnvironmentFile = context.GetValue(this.EnvironmentFileLocation);
        EnvironmentConfigInitialization EnvironmentInitialize = new EnvironmentConfigInitialization(EnvironmentFile);
        ConfigEnvironment EnvironmentDetail = EnvironmentInitialize.LoadData();
        return EnvironmentDetail;
    }
}

在工作流程的后续活动中,我想获取存储在此对象中的数据。但是,以下代码将出现编译错误,因为EnvironmentDetail对象找不到函数GetDatabaseConnectionString。

public sealed class ExecuteSQL : CodeActivity<DataRowCollection>
{
    // Define an activity input argument of type string
    public InArgument<string> SQLScript { get; set; }
    public InArgument<ConfigEnvironment> EnvironmentDetail { get; set; }

    // If your activity returns a value, derive from CodeActivity<TResult>
    // and return the value from the Execute method.
    protected override DataRowCollection Execute(CodeActivityContext context)
    {

        string connectionString4 = EnvironmentDetail.GetDatabaseConnectionString(); //This create a compile error

}

}

编译警告如下 'System.Activities.InArgument'不包含'GetDatabaseConnectionString'的定义,并且没有扩展方法'GetDatabaseConnectionString'接受类型为'System.Activities.InArgument'的第一个参数'(您是否缺少using指令或程序集引用) ?)

1 个答案:

答案 0 :(得分:0)

事实证明,EnvironmentDetail属于InArgument类型(或InArgument<ConfigEnvironment>)但未属于ConfigEnvironment类型您需要执行context.Get<ConfigEnvironment>()才能获得类型ConfigEnvironment的变量。

如果这可以解决您的问题,或者是否还有其他问题,请告诉我们; - )