从.NET程序集加载项目设置 [英] Load Project Settings from .NET Assembly

查看:153
本文介绍了从.NET程序集加载项目设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET C#2008赢表单解决方案多个项目。其中的一些项目有在其中定义设置(通过项​​目属性,设置UI部分 - 而不是一个配置文件)。我试图建立自己的应用程序的调试部分(隐藏远离用户),将然后显示了这些设置是当前设置为。我有以下code,获取所有的组件:

I have multiple projects in a .NET C# 2008 Win Forms Solution. Some of these projects have settings that are defined in them (through the project properties, settings UI portion - not a config file). I am attempting to build a debug portion of my application (hidden away from the users) that would then display what these settings are currently set at. I have the below code that gets all of the assemblies:

AssemblyName[] assemComponents = Assembly.GetExecutingAssembly().GetReferencedAssemblies();

foreach (AssemblyName assemName in assemComponents)
{
     ListViewItem item = listComponents.Items.Add(assemName.Name);
     item.SubItems.Add(assemName.Version.ToString());                                
}

目前的code这个块只需把程序集名称的列表以及它们在控制版本。不过,我想知道,如果使用这些组件对象(或任何其他人对这个问题)我可以取回已设置的项目设置。

Currently this block of code just puts the list of assembly names and their version in a control. However, I would like to know if using these Assembly objects (or any others for that matter) I can retrieve the project settings that have been set.

感谢大家的帮助,请让我知道,如果我可以提供更多的细节。

Thank you all for your help and please let me know if I can provide additional detail.

推荐答案

当您添加项目到设置设计时,Visual Studio生成一个类的读/写这些设置到文件;它还包括用于访问该类保存的默认值实例的静态默认属性。 (请参阅您的项目Settings.Designer.cs)。

When you add items to the Settings Designer, Visual Studio generates a class for reading/writing those settings to a file; it also includes a static Default property for accessing an instance of this class that holds the default values. (See Settings.Designer.cs in your project).

您可以找到这些类和反思检查他们:

You can locate these classes and inspect them with reflection:

foreach ( AssemblyName assemName in assemComponents )
{
    foreach ( var type in Assembly.Load( assem ).GetTypes() )
    {
        // look for classes that derive from SettingsBase named "Settings"
        if ( type.Name == "Settings" && typeof( SettingsBase ).IsAssignableFrom( type ) )
        {
            // get the value of the static Default property
            var defaults = (SettingsBase)type.GetProperty( "Default" ).GetValue( null, null );

            // walk the property list and get the value from the Default instance
            foreach ( SettingsProperty prop in defaults.Properties )
            {
                Debug.WriteLine( string.Format( "{0}: {1}", prop.Name, defaults[ prop.Name ] ) );
            }
        }
    }
}

这篇关于从.NET程序集加载项目设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆