
3.2.8 Dictionary
字典(Dictionary)类型是一种Object类型,用于在程序中存储键值对。Dictionary的键值对由Key(键)和Value(值)两个元素组成,其中Key必须是唯一的,而Value不需要唯一,使用时可以通过Dictionary(Key)的形式在Dictionary中获取到Key对应的Value。
当程序中希望通过唯一标识保存或查询其对应的信息时,都可用Dictionary类型,例如药品价格单、图书馆的书籍目录、各国家首都信息等。
Dictionary类型不在Variables面板的Variable type下拉列表的默认选项中,它可以在Browse and Select a .Net Type对话框中的系统命名空间System.Collections.Generic下找到,如图3-121所示。Dictionary<TKey,TValue>中的TKey表示Key的数据类型,TValue表示Value的数据类型。

图3-121 Dictionary类型示例
Dictionary中的Key和Value都可以是任何类型,如String型、Int32型、Double型及其他.Net中的数据类型。
Dictionary本身作为一种Object类型,与之前提到的数据类型不同,需要实例化之后才可以使用。它的构造函数形式为new Dictionary(Of TKey,TValue) From {{Key1,Value1}, {Key2,Value 2}}。Dictionary型变量在Variables面板中的显示如图3-122所示。

图3-122 Dictionary型变量在Variables面板中的显示
在Studio中,获取Dictionary中的信息有如下几种常见方式。
□Dictionary(Key):获取与指定的Key相关联的Value值。
□Dictionary.Item(Key):获取与指定的Key相关联的Value值,同Dictionary(Key)。
□Dictionary.Keys(Index):获取指定下标的Key值。
□Dictionary.Values(Index):获取指定下标的Value值。
□Dictionary.Count:获取包含在Dictionary<TKey,TValue>中的键值对的数目。
□Dictionary.Keys:获取包含在Dictionary<TKey,TValue>中的Key的集合。
□Dictionary.Values:获取包含在Dictionary<TKey,TValue>中的Value的集合。
要对Dictionary中的信息进行判断时,可以使用如下方法。
□Dictionary.ContainsKey(Key):确定Dictionary<TKey,TValue>是否包含指定的Key。
□Dictionary.ContainsValue(Value):确定Dictionary<TKey, TValue>是否包含特定Value。
□Dictionary.Equals(Dictionary):确定指定的Dictionary是否等于当前的Dictionary。
需要更改或追加Dictionary中的键值对时,可以使用Assign活动将Value值赋值给Dictionary(Key)。如果指定的Key已经存在,则会将它相关联的Value修改为新的Value值;如果指定的Key不存在,则会在Dictionary中追加新的Key和Value值。
【例3.13】使用Dictionary变量完成修改、添加和打印各国家首都的流程。创建一个项目,创建Dictionary型变量CapitalInfo,在Dictionary中设置默认值(Key:中国,Value:BeiJing)。将中国的首都信息由英文“BeiJing”更改为中文“北京”,添加日本的首都为东京的信息到Dictionary中,并在Output面板中输出中国和日本的首都。流程图如图3-123所示。
具体实现步骤如下所示。
1)进入Studio界面,点击Process创建一个新流程,命名为3_13_Dictionary,如图3-124所示。

图3-123 流程图

图3-124 新建流程
2)拖入一个Sequence活动到设计器面板。在Properties面板中,将Sequence活动的DisplayName属性更改为“首都管理流程”,如图3-125所示。
3)在Variables面板中创建Dictionary型变量CapitalInfo,用于存储个国家首都信息。默认的Variable type下拉列表中不含有Dictionary型,可以选择Browse for Types...选项,如图3-126所示。

图3-125 拖入Sequence活动并更改属性

图3-126 创建变量并更改数据类型
4)系统随即会显示Browse and Select a.Net Type对话框,在Type Name字段中输入想要查找的变量类型关键字dictionary,如图3-127所示。在结果中选择需要的选项Dictionary<TKey,TValue>,此时对话框上方会显示需要为Dictionary的Key和Value选择数据类型,在Key和Value对应的下拉列表中都选择String后点击OK按钮,如图3-128所示。

图3-127 根据关键字查找变量类型

图3-128 选择字典类型
5)此时Variables面板中变量CapitalInfo的数据类型被更改为Dictionary型,Key和Value的数据类型都为String型。在Variables面板的Default字段中输入new Dictionary(Of String,String) From {{"中国","BeiJing"}},为变量CapitalInfo初始化并设置默认值,如图3-129所示。

图3-129 变量初始化及设置默认值
6)拖入一个Assign活动到“首都管理流程”活动中。在Properties面板中,将Display-Name属性更改为“更改首都BeiJing为中文”,在To属性中输入CapitalInfo("中国"),在Value属性中输入“"北京"”,如图3-130所示。

图3-130 更改首都BeiJing为中文
7)再拖入一个Assign活动到“首都管理流程”活动中。在Properties面板中,将DisplayName属性更改为“添加日本首都信息”,在To属性中输入CapitalInfo("日本"),在Value属性中输入“"东京"”,如图3-131所示。

图3-131 添加日本首都信息
8)拖入一个Write Line活动到“首都管理流程”活动中。在Properties面板中,将DisplayName属性更改为“输出中国和日本首都”,将Text属性更改为“"中国首都:"+CapitalInfo("中国")+"。"+"日本首都:"+CapitalInfo("日本")+"。"”,如图3-132所示。

图3-132 输出中国和日本首都活动设置
9)按F5键执行流程,将在Output面板中显示执行结果,如图3-133所示。

图3-133 执行结果
除此之外,还可以使用Dictionary.Add(Key,Value)方法将指定的键值对添加到Dictionary中。这种方法需要借助特定的活动来实现,如下所示。
活动1:使用Invoke Method活动添加键值对。
UiPath是基于.Net开发的,因此.Net中的程序方法基本都可以在UiPath中使用。
Invoke Method活动是UiPath提供的可以调用目标方法的活动,将目标和需要导入/导出的参数写在该活动的Properties面板中,即可调用方法的对目标进行操作。
当我们想要通过调用.Net方法的方式来实现需求时,就可以使用Invoke Method活动,具体使用方法如例3.14所示。
【例3.14】使用Invoke Method活动完成添加和打印各国家首都的流程。创建一个项目,创建Dictionary型变量CapitalInfo,添加中国的首都为北京的信息到Dictionary中,并在Output面板输出中国的首都。流程图如图3-134所示。

图3-134 流程图
具体实现步骤如下所示。
1)进入Studio界面,点击Process创建一个新流程,命名为3_14_Dictionary_InvokeMethod,如图3-135所示。

图3-135 新建流程
2)参照例3.13中的第2~6步创建项目,完成后如图3-136所示。

图3-136 参照例3.13创建项目
3)拖入一个Assign活动到“首都管理流程”活动中。在Properties面板中,将DisplayName属性更改为“初始化字典”,在To属性中输入变量CapitalInfo,在Value属性中输入new Dictionary(Of String,String),如图3-137所示。

图3-137 初始化字典
4)在Activities面板的搜索框内输入invoke method,如图3-138所示。

图3-138 搜索Invoke Method活动
5)将Invoke Method活动拖入“首都管理流程”活动中。在Properties面板中,将DisplayName属性更改为“添加中国首都信息”,在MethodName属性中输入Add,在TargetObject属性中输入变量CapitalInfo,如图3-139所示。

图3-139 拖入Invoke Method活动并更改属性
6)在Properties面板中点击Parameters右边的设置按钮,系统将会弹出Parameters窗口,点击Create Arguments按钮创建两个In方向的String型参数,Value分别为"中国"和"北京",如图3-140所示。

图3-140 设置Invoke Method活动的参数
7)拖入一个Write Line活动到“首都管理流程”活动中。在Properties面板中,将DisplayName属性更改为“输出中国首都”,将Text属性更改为“"中国首都:"+CapitalInfo("中国")+"。"”,如图3-141所示。
8)按F5键执行流程,将在Output面板中显示执行结果,如图3-142所示。

图3-141 输出中国首都活动设置
活动2:使用Invoke Code活动添加键值对。
Invoke Code活动是UiPath提供的可以直接调用C#或.Net代码的活动,将需要执行的代码和需要导入/导出的参数写在该活动的Properties面板中,选择对应的语言类型,即可执行要调用的代码。

图3-142 执行结果
当我们想要通过调用代码的方式来实现需求时,就可以使用Invoke Code活动,具体使用方法如例3.15所示。
【例3.15】使用Invoke Code活动完成例3.14的需求。
具体实现步骤如下所示。
1)进入Studio界面,点击Process创建一个新流程,命名为3_15_Dictionary_InvokeCode,如图3-143所示。

图3-143 新建流程
2)参照例3.13中的第2~6步创建项目,完成后如图3-144所示。

图3-144 参照例3.13创建项目
3)拖入一个Assign活动到“首都管理流程”活动中。在Properties面板中,将DisplayName属性更改为“初始化字典”,在To属性中输入变量CapitalInfo,在Value属性中输入new Dictionary(Of String,String),如图3-145所示。

图3-145 初始化字典
4)在Activities面板的搜索框内输入invoke code,如图3-146所示。

图3-146 搜索Invoke Code活动
5)将Invoke Code活动拖入“首都管理流程”活动中。在Properties面板中,将DisplayName属性更改为“添加中国首都信息”,在Code属性中输入CapitalInfo.Add("中国","北京"),如图3-147所示。

图3-147 拖入Invoke Code活动并更改属性
6)在Properties面板中点击Arguments右边的设置按钮,系统将会弹出Arguments窗口,点击Create Arguments按钮创建In方向的参数CapitalInfo,数据类型与之前的变量CapitalInfo一致,将变量CapitalInfo写入Value字段后点击OK按钮,如图3-148所示。

图3-148 设置Invoke Code活动的参数
7)拖入一个Write Line活动到“首都管理流程”活动中。在Properties面板中,将DisplayName属性更改为“输出中国首都”,将Text属性更改为“"中国首都:"+CapitalInfo("中国")+"。"”,如图3-149所示。
8)按F5键执行流程,将在Output面板中显示执行结果,如图3-150所示。
当我们需要从Dictionary中移除键值对时,可以使用Dictionary.Remove(Key,Value)方法来移除指定的键值对,也可以使用Dictionary.Clear()方法来移除所有的键值对。这两种方法都需要借助特定的活动来实现,具体方式请参照使用Dictionary.Add(Key,Value)方法添加键值对的案例。

图3-149 输出中国首都活动设置

图3-150 执行结果

图3-151 流程图
在实际项目中,我们有时会需要对Dictionary进行遍历,实现方法见例3.16。
【例3.16】完成遍历Dictionary的流程。创建一个项目,将中国、日本、美国的首都信息存入Dictionary型变量,遍历Dictionary并在Output面板输出该Dictionary中所有的首都信息。流程图如图3-151所示。
具体实现步骤如下所示。
1)进入Studio界面,点击Process创建一个新流程,命名为3_16_Dictionary_Traversal,如图3-152所示。
2)参照例3.13中的第2~6步创建项目,完成后如图3-153所示。
3)拖入一个Assign活动到“首都管理流程”活动中。在Properties面板中,将DisplayName属性更改为“在字典中添加各国首都信息”,在To属性中输入变量Capital-Info,在Value属性中输入New Dictionary(Of String,String) From {{"中国","北京"},{"日本","东京"},{"美国","华盛顿"}},如图3-154所示。

图3-152 新建流程

图3-153 参照例3.13创建项目

图3-154 在字典中添加各国首都信息
4)在Activities面板的搜索框内输入for each,如图3-155所示。

图3-155 搜索For Each活动
5)将For Each活动拖入“首都管理流程”活动中。在Properties面板中,将DisplayName属性更改为“遍历字典”,将TypeArguments属性更改为String,在Values属性中输入变量CapitalInfo.Values,该活动会遍历CapitalInfo中的所有Value值,如图3-156所示。

图3-156 更改For Each活动的属性
6)拖入一个Write Line活动到“遍历字典”活动中。在Properties面板中,将DisplayName属性更改为“输出首都”,在Text属性中输入item,如图3-157所示。
7)按F5键执行流程,将在Output面板中显示执行结果,如图3-158所示。

图3-157 输出首都活动设置

图3-158 执行结果