如何替换/覆盖同一命名空间中的类
基本上,我在我的网站中定义了一个类,其中包含一堆枚举来定义一些常量。每当网站更新到新版本时,它将(并且应该)使用原始的类定义。在某些罕见的情况下,我需要覆盖每个网站的这些枚举的值。所以我想我可以将一个自定义文件放入网站中,用“覆盖”语句重新定义原始类,以便原始文件始终保持不变,但不会被使用。
然而,当我添加文件时,它当然会抛出错误:“className”已经包含“enumName”的定义。
任何帮助/想法将不胜感激。
示例:
public enum PageIDs
{
Login = 2,
SearchResults = 3,
DocumentsLibrary = 4,
Error = 5,
PageNotFound = 6,
NewsAndEvents = 7
}
应替换为:
public enum PageIDs
{
Login = 2,
SearchResults = 3,
DocumentsLibrary = 4,
Error = 9,
PageNotFound = 15,
NewsAndEvents = 17
}
Basically, I have a class defined in my website which contains a bunch of Enums to define some constant numbers. Whenever the website updates to a new version, it will (and should) use the original class definition. In some rare cases I need to overwrite the values of these Enums per website. So I was thinking I could drop a customised file into the website that redefines the original class with the "override" statement, so that the original file will always stay the same, but not get used.
HOWEVER, when I add the file it of course throws the error: 'className' already contains a definition for 'enumName'.
Any help/ideas would be greatly appreciated.
Example:
public enum PageIDs
{
Login = 2,
SearchResults = 3,
DocumentsLibrary = 4,
Error = 5,
PageNotFound = 6,
NewsAndEvents = 7
}
should be replaced by:
public enum PageIDs
{
Login = 2,
SearchResults = 3,
DocumentsLibrary = 4,
Error = 9,
PageNotFound = 15,
NewsAndEvents = 17
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这些值发生变化,您最好将应用程序设计为可配置的。您可以使用 Web 配置文件或其他配置 xml 或 DB。
if these values are changing you better design your application as configurable. you can use web config file or other configuration xml or DB.
当使用可被视为“常量”的枚举时,通常不会更改这些值。
您的选择是创建一个具有 virtual int 属性的类,或者加载一些配置文件,如 UnhandledException 提到的。
使用下面的解决方案,您需要实例化该类。
如果您想要静态 getter,您将无法创建继承,而应该从其他地方加载您的整数,例如从上述类的配置中加载。
When using enums which can be seen as "constants", you don't normally change the values.
Your option is to either create a class with virtual int properties or to load some configuration file as UnhandledException mentions.
With the solution below you need to instantiate the class
If you would like static getters you won't be able to create an inheritance, but should rather load your ints from somewhere else, either from configuration of from the above classes for instance.