如何替换/覆盖同一命名空间中的类

发布于 2024-12-13 20:18:47 字数 609 浏览 3 评论 0原文

基本上,我在我的网站中定义了一个类,其中包含一堆枚举来定义一些常量。每当网站更新到新版本时,它将(并且应该)使用原始的类定义。在某些罕见的情况下,我需要覆盖每个网站的这些枚举的值。所以我想我可以将一个自定义文件放入网站中,用“覆盖”语句重新定义原始类,以便原始文件始终保持不变,但不会被使用。

然而,当我添加文件时,它当然会抛出错误:“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失与倦" 2024-12-20 20:18:47

如果这些值发生变化,您最好将应用程序设计为可配置的。您可以使用 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.

停顿的约定 2024-12-20 20:18:47

当使用可被视为“常量”的枚举时,通常不会更改这些值。

您的选择是创建一个具有 virtual int 属性的类,或者加载一些配置文件,如 UnhandledException 提到的。

使用下面的解决方案,您需要实例化该类。

public class BasePageIDs {
    public virtual int Login { get { return 2; } }
    public virtual int Search { get { return 3; } }
}

public class SpecializedPageIDs : BasePageIds {
    public override int Search { get { return 4; } }
}

如果您想要静态 getter,您将无法创建继承,而应该从其他地方加载您的整数,例如从上述类的配置中加载。

public static class PageIDs {
    public static int Login { get { /* load from configuration */ return config; } }
}

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

public class BasePageIDs {
    public virtual int Login { get { return 2; } }
    public virtual int Search { get { return 3; } }
}

public class SpecializedPageIDs : BasePageIds {
    public override int Search { get { return 4; } }
}

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.

public static class PageIDs {
    public static int Login { get { /* load from configuration */ return config; } }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文