C# 类型可用性 - 跨应用程序使用应用程序设置架构 - 强类型配置

发布于 2024-10-06 03:03:28 字数 1485 浏览 0 评论 0原文

我希望编写一个统一的配置类,用于聚合 Visual Studio 的应用程序设置体系结构 (ASA) 中存在的多个应用程序中的设置。 (http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx)

ASA 为每个项目生成一个 Settings.Designer.cs 类。我想做的是将多个项目的所有设置类聚合在一起,以便从一个类中使用。例如,我有一个仅用于存储共享设置的项目,仅此而已。该项目有一个类似以下的类:

public static class ConfigManager
{
 public static TopNamespace.Configuration.Properties.Settings Shared
 {
  get { return TopNamespace.Configuration.Properties.Settings.Default; }
 }
}

这允许我访问如下配置:

ConfigManager.Shared.ConfigKey

我想做的是扩展此 ConfigManager 类,以便我可以访问其他项目生成的设置类。我不确定这是否可能或如何去做。例如,我希望能够执行以下操作:

ConfigManager.ApplicationName.ConfigKey

ConfigManager.OtherApplicationName.ConfigKey

循环引用阻止我从配置项目添加对应用程序项目的引用,以访问类型以创建更多成员,例如:

public static class ConfigManager
{
 public static TopNamespace.Configuration.Properties.Settings ApplicationName
 {
  get { return TopNamespace.ApplicationName.Properties.Settings.Default; }
 }
 public static TopNamespace.Configuration.Properties.Settings OtherApplicationName
 {
  get { return TopNamespace.OtherApplicationName.Properties.Settings.Default; }
 }
}

然后尝试添加对配置项目的引用从应用程序项目返回以访问 ConfigManager 类。

我考虑过扩展类,但部分类不能跨程序集工作,并且不能从静态类继承。使用泛型似乎也不是一个非常好的解决方案,因为您无法为属性成员提供一个干净的 ConfigManager.ApplicationName.SettingName 类型的签名。

有没有办法使用继承或对应用程序域中的可用类型进行某种动态检查,并加载可用应用程序的设置类型以从单个 ConfigManager 类中使用?使 Visual Studio 生成的跨多个项目的所有设置类可从单个管理器类中使用,并具有每个项目设置实例的属性。

I am looking to write a unified configuration class that aggregates the settings in multiple applications which exist from within Visual Studio's Application Settings Architecture (ASA). (http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx)

The ASA generates a Settings.Designer.cs class for each project. What I'd like to do is aggregate all of the settings classes of several projects together to be available from one class. For instance I have a project that's only used to store shared settings and that's it. This project has a class like:

public static class ConfigManager
{
 public static TopNamespace.Configuration.Properties.Settings Shared
 {
  get { return TopNamespace.Configuration.Properties.Settings.Default; }
 }
}

This allows me to access config like:

ConfigManager.Shared.ConfigKey

What I'd like to do is extend this ConfigManager class to give me access to the generated settings classes of other projects. I'm not sure if this is possible or how to go about doing this. For instance I'd like to be able to do:

ConfigManager.ApplicationName.ConfigKey

ConfigManager.OtherApplicationName.ConfigKey

Circular references prevent me from adding references to the application projects from the Configuration project to get access to the types to make more members like:

public static class ConfigManager
{
 public static TopNamespace.Configuration.Properties.Settings ApplicationName
 {
  get { return TopNamespace.ApplicationName.Properties.Settings.Default; }
 }
 public static TopNamespace.Configuration.Properties.Settings OtherApplicationName
 {
  get { return TopNamespace.OtherApplicationName.Properties.Settings.Default; }
 }
}

And then trying to add a reference to the Configuration project back from the application projects to gain access to the ConfigManager class.

I thought about extending the classes but partial classes won't work across assemblies and you can't inherit from static classes. Using generics doesn't seem like it'd be a very good solution either since you couldn't have a nice clean ConfigManager.ApplicationName.SettingName type of signatures for the property members.

Is there any way to use inheritance or do some kind of dynamic examination of available types in the Application Domain and load the settings types for available applications to be made available from a single ConfigManager class? Making all the settings classes across several projects generated by visual studio available from one single manager class with properties for each project settings instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

享受孤独 2024-10-13 03:03:28

我最终决定设置它的方法是使用一个“静态”类,但实际上并没有以这种方式定义,因为.Net 不允许您继承静态类型。但是您可以编写包含所有静态成员的类并使用受保护的构造函数,这使其在所有意图和目的上都是静态的。然后,您可以从外部项目类继承,例如:

public class ConfigManager : ExternalProjectNamespace.ConfigManager

使用此约定,我能够获得所需的语法,而无需在将使用 ConfigManager 的每个项目中重复代码。

The way I finally settled on setting this up is using a class that was 'static' but not really defined that way because .Net doesn't let you inherit static types. But you can write the class with all static members and use a protected constructor, which makes it for all intents and purposes static. Then you can inherit from your external project class like:

public class ConfigManager : ExternalProjectNamespace.ConfigManager

Using this convention I was able to get the syntax I wanted without having to duplicate code in every single project that would be using the ConfigManager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文