C#自动化测试:如何通过字符串参数修改对象名称?
我正在使用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
orderNumberinputs. - Reflection has a tiny performance hit, but it's totally negligible for automation testing workflows.
2. Refactor Your Repo Object (Recommended for Maintainability)
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

