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 TopNamespa开发者_开发问答ce.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.
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.
精彩评论