[英]Regarding C# Static Readonly members
I have the following situation. 我有以下情况。 There is some very common class in my application that contains a static readonly field called "BinDirectory" that holds the path to the bin directory.
我的应用程序中有一些非常常见的类,它包含一个名为“BinDirectory”的静态只读字段,它保存bin目录的路径。 Other fields in this class, which are static readonly too, use this value to as a base to their value.
此类中的其他字段(也是静态只读)也使用此值作为其值的基础。 On the current version BinDirectory is initialized to hold the directory where the code is running (ie Assembly.GetExecutingAssembly().CodeBase).
在当前版本上,BinDirectory被初始化为保存代码运行的目录(即Assembly.GetExecutingAssembly()。CodeBase)。 I want to extend this class to initialize BinDirectory to hold the "TargetDir" from the installer context when it is run from my application installer.
我想扩展此类以初始化BinDirectory,以便在从我的应用程序安装程序运行时从安装程序上下文中保存“TargetDir”。 I can change BinDirectory to be simply static but I don't want to, since it will make me do lots of changes to a class that is common in my app.
我可以将BinDirectory改为静态但我不想,因为它会让我对我的应用程序中常见的类进行大量更改。 Can somebody suggest an elegant solution to this problem?
有人可以为这个问题提出一个优雅的解决方案吗?
Make it a property with just the "get" accessor: 使用“get”访问器使其成为一个属性:
public static string BinDirectory
{
get { return _initialisedBinDirectory; }
}
Then in your static constructor code, initialise the private variable as you need. 然后在静态构造函数代码中,根据需要初始化私有变量。
EDIT 编辑
Delayed load (as per comment): 延迟加载(根据评论):
public static string BinDirectory
{
get
{
if (_initialisedBinDirectory == null)
// load the variable when needed
else
return _initialisedBinDirectory;
}
}
This way you only load the variable when you need it, and it's re-used whenever you call it again. 这样您只需在需要时加载变量,并且只要再次调用它就会重新使用它。 Hopefully you don't class
null
as a valid value for it though. 希望你不要将
null
作为它的有效值。
This is what AppConfigs are for. 这就是AppConfigs的用途。 In your AppSettings section, add a new key called
BinDirectory
. 在AppSettings部分中,添加一个名为
BinDirectory
的新密钥。 You can re-write your class as: 你可以重新编写你的课程:
public static string BinDirectory
{
get
{
return ConfigurationManager.AppSettings["BinDirectory"];
}
}
Finally, as one of the last steps in your installation process, you can change the BinDirectory to point to any directory you want. 最后,作为安装过程的最后一步,您可以将BinDirectory更改为指向所需的任何目录。 So now this value is determined entirely by the installer context.
所以现在这个值完全由安装程序上下文决定。
It sounds like you're loath to change a static readonly field to simply static because it would force you to change the initialization of all of the other static readonly fields in your class. 听起来你不愿意将静态只读字段更改为静态,因为它会强制您更改类中所有其他静态只读字段的初始化。
If that is correct, unfortunately there isn't a whole lot you can do but take the time to make the change. 如果这是正确的,不幸的是,你可以做很多事情,但要花时间做出改变。 By allowing the BinDirectory field to be set at runtime you are fundamentally changing the initialization sequence of the fields.
通过允许在运行时设置BinDirectory字段,您将从根本上改变字段的初始化顺序。 Your code will need to adapt.
您的代码需要适应。
I think the easiest way is to convert to using static readonly properties which do the calculation of the value on the fly. 我认为最简单的方法是转换为使用静态只读属性,这些属性可以动态计算值。
For example: 例如:
public class Values {
public static string BinDir;
public static string OtherDir {
get { return Path.Combine(BinDir,@"Some\Other\Path"); }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.