studio
latest
false
重要 :
新发布内容的本地化可能需要 1-2 周的时间才能完成。
UiPath logo, featuring letters U and I in white

Studio 用户指南

上次更新日期 2025年9月25日

最佳实践

为确保编码自动化高效、可维护且可扩展,您需要遵循其最佳实践。本节概述了在您开始利用代码构建自动化解决方案时要记住的关键最佳实践。采用这些最佳实践将帮助您设计编码自动化,尤其是在构建代码结构、实施错误处理策略或创建可重用组件时。

将模板与低代码自动化配合使用

请注意,如果您正在低代码自动化 (XAML) 中使用来自代码源文件 (CS) 的类型或方法,在创建项目时使用的模板中包含这些类型或方法,或在模板中导出此类文件,可能会破坏 XAML 文件中的命名空间。此中断会导致您的项目无法在 Studio 中运行。

在代码源文件中用作参数的对象

在代码源文件中用作参数的数据对象必须具有空构造函数。 否则,Studio 无法对工作流进行序列化。

并行性

并行可以提高性能,在编码自动化中实施更加容易。 Parallel.ForEach 循环可以提高自动化的执行速度。

嵌套类

编码自动化 (CS 文件) 不支持在低代码自动化 (XAML 文件) 中使用或调用嵌套类的变量或参数。如果您尝试使用“调用工作流文件”活动调用这些类型的参数,或在 XAML 文件中创建此类参数,您将收到错误。我们建议仅在其他 CS 文件中使用嵌套类中的变量和参数。

编码工作流继承

编码工作流继承可提高代码的可重用性和模块性,允许您创建类层次结构,其中子类可以继承并扩展父类的功能。此继承层次结构有助于组织代码,并避免在多个类中复制常见功能。此外,它还简化了维护和更新,因为对父类所做的更改会自动传递给其子类,从而减少了引入错误或不一致的机会。
我们来演示如何使用三个类进行继承:CodedWorkflowParentFile(继承 CodedWorkflow 并包含自定义方法的中间类)和 MyCustomWorkflow 以及 AnotherCustomWorkflow(继承 ParentFile 的派生类)。下表显示了这些类和文件之间的继承方式:
编码自动化描述代码
CodedWorkflow (read-only partial class) 此类是所有编码工作流的基础。它包含继承自 CodedWorkflowBase 类的所有工作流通用的基本方法和属性。在此示例中,所有其他工作流最终都继承自该类。
重要提示:您希望其他工作流继承的文件还必须继承 CodedWorkflow 类,并隐式继承 CodedWorkflowBase 类。这可确保所有工作流继承基本功能并按预期工作。
public partial class CodedWorkflow : CodedWorkflowBase
    {
        public CodedWorkflow()
        {
         //...

    }
}public partial class CodedWorkflow : CodedWorkflowBase
    {
        public CodedWorkflow()
        {
         //...

    }
}
ParentFile (code source file containing an intermediate class and a custom method) 此类继承自 CodedWorkflow,并添加自定义方法和功能。在此示例中,它包含一个名为 CustomMethod 的自定义方法,该方法执行特定操作,例如在 Orchestrator 中启动作业。
public class ParentFile : CodedWorkflow
{
    public void CustomMethod(string processName, string folderPath, 
    StartProcessDtoJobPriority jobPriority, bool resumeOnSameContext, 
    out string jobId)
    {
        // Enter your custom code here.
        // For example, use the StartJob coded automation API inside this custom method.
        jobId = system.StartJob(processName, folderPath, jobPriority, resumeOnSameContext);
    }
}public class ParentFile : CodedWorkflow
{
    public void CustomMethod(string processName, string folderPath, 
    StartProcessDtoJobPriority jobPriority, bool resumeOnSameContext, 
    out string jobId)
    {
        // Enter your custom code here.
        // For example, use the StartJob coded automation API inside this custom method.
        jobId = system.StartJob(processName, folderPath, jobPriority, resumeOnSameContext);
    }
}
MyCustomWorkflow and AnotherCustomWorkflow (coded workflows that inherit the code source file) 这些类继承自 ParentFile,并通过覆盖方法或提供不同的参数值来进一步自定义工作流。在此示例中,我们有 MyCustomWorkflowAnotherCustomWorkflow,两者都继承 ParentFile
public class MyCustomWorkflow : ParentFile
    {
        [Workflow]
        public void Execute()
        {
            // You can now call CustomMethod from the base class.
            string processName = "YourProcessName";
            string folderPath = "YourFolderPath";
            StartProcessDtoJobPriority jobPriority = StartProcessDtoJobPriority.Normal;
            bool resumeOnSameContext = false;
            string jobId;

            // Call the custom method from the base class.
            CustomMethod(processName, folderPath, 
            jobPriority, resumeOnSameContext, out jobId);
        }
    }public class MyCustomWorkflow : ParentFile
    {
        [Workflow]
        public void Execute()
        {
            // You can now call CustomMethod from the base class.
            string processName = "YourProcessName";
            string folderPath = "YourFolderPath";
            StartProcessDtoJobPriority jobPriority = StartProcessDtoJobPriority.Normal;
            bool resumeOnSameContext = false;
            string jobId;

            // Call the custom method from the base class.
            CustomMethod(processName, folderPath, 
            jobPriority, resumeOnSameContext, out jobId);
        }
    }
总之,由于 ParentFile 继承自 CodedWorkflow,因此任何继承自 ParentFile 的类都会间接继承 CodedWorkflow 的功能和方法。换句话说,MyCustomWorkflowAnotherCustomWorkflow 通过中间类 ParentFile 以及其他自定义类(例如 CustomMethodCodedWorkflow 部分类继承核心功能。

注册自定义服务

要使用自定义逻辑增强编码自动化,您可以注册自定义服务,以便稍后在编码自动化中使用。请访问注册自定义服务,了解如何注册您自己的自定义服务。

“前”和“后”上下文

在测试用例中,“前”和“后”上下文允许您在运行测试用例之前和之后执行某些操作。这些上下文通常用于设置和关闭资源、执行日志记录以及管理测试环境。访问“前”和“后”上下文,了解上下文的行为以及如何实现它们。

技巧和窍门

使用以下提示和技巧设计高效且可靠的编码自动化。这些见解集合可帮助您优化代码,避免错误并最大限度地提高性能。
  • 我们建议您不要对命名空间进行任何更改。
  • 将操作存储在类中,并在整个项目中重用该操作,从而避免重复代码。
  • 您可以删除在设计期间导入但不再需要的命名空间。
  • 如果您需要从多个应用程序获取数据,请将编码自动化的各个阶段分开,这样您就不会混合来自不同来源的数据。

此页面有帮助吗?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath Logo
信任与安全
© 2005-2025 UiPath。保留所有权利。