使用 App.Config 导出要使用的 class/variables

Using App.Config to derive which class/variables to use

我正在构建一个 selenium 框架,它需要在不同的客户之间表现不同,例如2 位客户的网页会略有不同

我正在尝试从 app.config 应用程序设置中驱动它,例如

<add key="Customer" value="ABC" /> 

然后我有一个客户 classes:

public static class Customer
    {
        public static string CustomerName = ConfigurationManager.AppSettings["Customer"];
        public static string custAddress = {CustomerName}.custAddress ; //this is obviously wrong - how do i do this?...
    }

public static class ABC
    {
        custAddress = "customer abc address";

    }

public static class DEF
    {
        custAddress = "customer def address";

    }

然后我想以最简单的方式调用它来获取值,例如

var address = Customer.custAddress;

...将根据 app.config 设置进行设置。

我是一个 c# 新手,很抱歉不清楚 - 我一直在努力弄清楚如何将客户字符串转换成其同名的 class。我认为接口也可能是一种选择,但要与复杂性作斗争。有没有更好的方法?

我建议您为每个客户设置一组,地址只是 AppSettings["Address"]

这样您就不需要在添加第 3 个客户时修改您的代码。如果你保留 class ABC 那么设置就没有多大意义了。