App.Config自定义配置助手没有进行实例化 [英] App.Config custom configuration helper not instatiating

查看:93
本文介绍了App.Config自定义配置助手没有进行实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的app.config中为我正在构建的COM对象设置一个自定义部分,但是在花了几天之后没有运气;是的,几天;看看来自不同论坛的示例代码解释我仍然无法使其工作。这是我用来描述问题的简化代码:



ConfigurationTest.cs

I'm being trying to setup a custom section in my app.config for a COM object I'm building with no luck, after spending a couple days; yes a couple of days; looking at sample code explanations from different forums I still cannot make it work. This is a simplified code I put together to describe the issue:

ConfigurationTest.cs

using System.Runtime.InteropServices;
using System.Configuration;

namespace configurationTest
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ProgId("Utils.ConfigurationTest")]
    public class ConfigurationTest
    {
        public string passWord = "";

        public ConfigurationTest()
        {

        }

        public string getPassword()
        {
            configurationHelper config = (configurationHelper)ConfigurationManager.GetSection("credentials");
            this.passWord = config.PassWord;
            return this.passWord.ToString();
        }
    }
}



ConfigurationHelper.cs


ConfigurationHelper.cs

using System.Configuration;

namespace configurationTest
{
    public class configurationHelper : System.Configuration.ConfigurationSection
    {
        [ConfigurationProperty("passWord")]
        public string PassWord
        {
            get { return (string)this["passWord"]; }
            set { this["passWord"] = value; }
        }
    }
}





app.Config



app.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <section name="credentials" type="configurationTest.configurationHelper, ConfigurationHelper"/>
  </configSections>

  <credentials passWord ="PDm!@M!11"/>

</configuration>





我的问题是当我实例化COM对象并调用getPassword方法时,变量config为null 。我究竟做错了什么?欢迎提出任何想法,



谢谢



My problem is when I instantiate the COM object and call the getPassword method the variable "config" is null. What am I doing wrong? any ideas are welcome,

Thank you

推荐答案

为什么这么复杂地尝试?怎么样:

Why do you try it so complicately? What about:
<?xml version="1.0" encoding ="utf-8"?>
<configuration>
    <appSettings>
        <add key="passWord" value="abc123"/>
    </appSettings>
</configuration>



并在您的代码中:


and in your code:

this.passWord = System.Configuration.ConfigurationManager.AppSettings["passWord"];

事实证明问题是COM对象,COM对象不使用配置文件,

看到这个发布



http://preview.tinyurl.com/ofjqcbd [ ^ ]
It turns out that the problem is the COM object, COM object don't use config files,
see this posting

http://preview.tinyurl.com/ofjqcbd[^]


这篇关于App.Config自定义配置助手没有进行实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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