You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#自动化测试:如何通过字符串参数修改对象名称?

Can I dynamically modify object names by passing a string in C# for automation testing?

我正在使用C#进行自动化测试,想了解是否可以传入字符串值来修改对象名称?具体问题涉及代码中的repo.MainWindow.A201Info这一行,我希望传入orderNumber(如A201、A202、A203等),将A201Info修改为A202Info、A203Info等,请问这是否可行?

public static bool ChangeAndVerifyNewPriority(string orderNumber, string newPriority)
{
    bool priorityChanged = false;
        if(repo.SomeElement.SelfInfo.Exists(3000)){
            Utilities.DoubleClick(repo.MainWindow.A201Info);
            Utilities.TypeText(repo.Orders.PriorityTextBoxInfo, newPriority);
            Utilities.LeftClick(repo.Orders.ButtonOKInfo);
            return priorityChanged=true;
        }
        else{
            return priorityChanged=false;
        }   
}

Absolutely, you can do this! There are a couple of practical approaches to dynamically access properties like A201Info based on a string input in C#:

1. Use Reflection

Reflection lets you inspect and interact with types and properties at runtime, which fits your scenario perfectly. Here's how to adjust your method to use it:

public static bool ChangeAndVerifyNewPriority(string orderNumber, string newPriority)
{
    bool priorityChanged = false;
    if(repo.SomeElement.SelfInfo.Exists(3000))
    {
        // Build the target property name (e.g., "A201" → "A201Info")
        string propertyName = $"{orderNumber}Info";
        
        // Retrieve the property from repo.MainWindow via reflection
        var property = repo.MainWindow.GetType().GetProperty(propertyName);
        if (property == null)
        {
            // Handle invalid order numbers to avoid runtime crashes
            throw new ArgumentException($"No matching info property found for order: {orderNumber}");
        }
        
        // Get the actual info object from the property
        var orderInfo = property.GetValue(repo.MainWindow);
        
        // Proceed with your automation steps
        Utilities.DoubleClick(orderInfo);
        Utilities.TypeText(repo.Orders.PriorityTextBoxInfo, newPriority);
        Utilities.LeftClick(repo.Orders.ButtonOKInfo);
        priorityChanged = true;
    }
    return priorityChanged;
}

Quick notes on reflection:

  • Always add error handling (like the null check above) to account for invalid orderNumber inputs.
  • Reflection has a tiny performance hit, but it's totally negligible for automation testing workflows.

If you have control over the repo or MainWindow codebase, a cleaner and safer approach is to add an indexer or helper method to retrieve the order info directly. This avoids reflection entirely:

First, update your MainWindow class:

public class MainWindow
{
    // Existing properties: A201Info, A202Info, A203Info...
    
    // Add an indexer to fetch info by order number
    public object this[string orderNumber]
    {
        get
        {
            return orderNumber switch
            {
                "A201" => A201Info,
                "A202" => A202Info,
                "A203" => A203Info,
                // Add other order number cases here
                _ => throw new ArgumentException($"Unknown order number: {orderNumber}")
            };
        }
    }
}

Then your test method becomes much simpler and more readable:

public static bool ChangeAndVerifyNewPriority(string orderNumber, string newPriority)
{
    bool priorityChanged = false;
    if(repo.SomeElement.SelfInfo.Exists(3000))
    {
        var orderInfo = repo.MainWindow[orderNumber];
        Utilities.DoubleClick(orderInfo);
        
        Utilities.TypeText(repo.Orders.PriorityTextBoxInfo, newPriority);
        Utilities.LeftClick(repo.Orders.ButtonOKInfo);
        priorityChanged = true;
    }
    return priorityChanged;
}

Why this is better:

  • No reflection overhead.
  • Compile-time validation: typos in order numbers will be caught during development, not at runtime.
  • Easier for your team to read, maintain, and extend later.

内容的提问来源于stack exchange,提问作者MrRabbit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 16:56:14