简体   繁体   English

.Net-将Settings.settings文件中的设置设置为null

[英].Net - Set a Setting in Settings.settings file to null

Question

Is it possible to override the value of a settings created in a settings file (Settings.settings) to be null in a app.config or web.config? 是否可以将在设置文件(Settings.settings)中创建的设置的值覆盖为app.config或web.config中的null

Scenario 脚本

I have a project ( SO.Example ) that has a Settings.setting file that defines a setting SetMeToNull : 我有一个项目( SO.Example ),该项目具有一个Settings.setting文件,该文件定义了设置SetMeToNull

Settings.setting的屏幕截图

I have another project ( SO.Example.Web ) that uses SO.Example . 我还有另一个使用SO.Example项目( SO.Example.Web )。 I would like to set SetMeToNull to null in the So.Exmaple.Web 's web.config: 我想设置SetMeToNull在空So.Exmaple.Web的web.config文件:

   <SO.Example.Properties.Settings>
        <setting name="SetMeToNull" serializeAs="String">
            <value>[[NULL]]</value>
        </setting>
    </SO.Example.Properties.Settings>

Obviously there are ways to work around this if it's not possible, but it would be convenient if it was possible. 显然,如果不可能的话,有多种方法可以解决此问题,但如果可能的话,将很方便。

Justification 理由

This may seam like an odd requirement when in most cases an empty string could comfortably sit in for a null string. 在大多数情况下,空字符串可以舒适地放入空字符串时,这可能像是一个奇怪的要求。 At the moment, however, I'm faced with a case where a null string does not have the same meaning as an empty string. 但是,目前,我面临的情况是空字符串与空字符串的含义不同。 For the sake of the argument, let's just say SO.Example is a 3rd party library and I have no control over it. 为了论证,我们只说SO.Example是一个3rd party库,但我对此没有控制权。 (Ie refactoring it would take more time than it's worth). (即重构将花费更多的时间而不是值得的)。

You can open the settings file with the XML editor instead of the default editor and set it manually to: 您可以使用XML编辑器而不是默认编辑器打开设置文件,并将其手动设置为:

 <Setting Name="Setting" Type="System.String" Scope="User">
  <Value Profile="(Default)"></Value>
 </Setting>

But I wonder if it makes any sense to have a setting set to null.. 但是我想知道将设置设置为null是否有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM