首先对令人困惑的标题感到抱歉,我可以找到准确的词来描述这种情况。但是通过一个例子很容易理解。
我们有一个静态类,它将表名作为目录保存,如下所示:
public static class Tables
{
public const string Employees= "DBEmploy1";
public const string Sales = "Sale1";
(...etc)
}
并在我们的代码中使用它们,如下所示:
string sql="select name, surname from " + Tables.Employees + " where code='1'"
但有时我们需要为数据库连接或表添加其他前缀/后缀。当前的解决方案是声明第二个表目录:
public static class CurrentDB1Prefix="[databasex].dbo."
public static class Tables
{
public const string Employees= "DBEmploy1";
public const string Sales = "Sale1";
(...etc)
}
public static class TablesP
{
public static readonly string Employees= CurrentDB1Prefix + Employees;
public static readonly string Sales = CurrentDB1Prefix + Sales;
(...etc)
}
并在我们的代码中使用它们,例如:
string sql="select name, surname from " + TablesP.Employees + " where code='1'"
为了节省维护 2 个表列表的工作量,我们想做这样的事情:
public static class CurrentDB1Prefix="[databasex].dbo."
public static class Tables
{
public const string Employees= "DBEmploy1";
public const string Sales = "Sale1";
(...etc)
}
public static class TablesP
{
//this would return the above Table class variables with the prefixes somehow
return CurrentDB1Prefix + Table.TableVariablex;
}
如何才能做到这一点?或者像这样可用的一些近似值?