[Mono-list] Patch for <remove/> support in .config files

Jonathan Pryor jonpryor@vt.edu
12 Dec 2002 21:07:34 -0500


--=-25FBwe9m5oyQx7t2vtCy
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Attached is a patch to fix <remove/> support in .config files.

The problem, in summary, is that the test against `removedMark' is
incorrect.  The stored factory object isn't set to `removedMark' until
after the test (ConfigurationSettings.cs:380), so a section could never
be set to `removedMark' under the current code.

Also attached is a .config file sample program I'm working on that
illustrates proper behavior.  With the patch applied the programs
completes.  Without the patch a `ConfigurationException' is generated
from the <remove/> statement in the .config file.

Should I apply this?

 - Jon


--=-25FBwe9m5oyQx7t2vtCy
Content-Disposition: attachment; filename=config-settings.diff
Content-Type: text/x-patch; name=config-settings.diff; charset=UTF-8
Content-Transfer-Encoding: 7bit

Index: mcs/class/System/System.Configuration/ConfigurationSettings.cs
===================================================================
RCS file: /cvs/public/mcs/class/System/System.Configuration/ConfigurationSettings.cs,v
retrieving revision 1.16
diff -u -r1.16 ConfigurationSettings.cs
--- mcs/class/System/System.Configuration/ConfigurationSettings.cs	4 Nov 2002 23:49:25 -0000	1.16
+++ mcs/class/System/System.Configuration/ConfigurationSettings.cs	13 Dec 2002 02:01:32 -0000
@@ -374,7 +374,7 @@
 				removeValue = sectionName + '/' + removeValue;
 
 			object o = LookForFactory (removeValue);
-			if (o != null && o != removedMark)
+			if (o != null && o == removedMark)
 				ThrowException ("No factory for " + removeValue, reader);
 
 			factories [removeValue] = removedMark;

--=-25FBwe9m5oyQx7t2vtCy
Content-Disposition: attachment; filename=switch-test.exe.config
Content-Type: text/plain; name=switch-test.exe.config; charset=UTF-8
Content-Transfer-Encoding: 7bit

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="remove-me"
      type="System.Configuration.IgnoreSectionHandler"/>
    <remove name="remove-me"/>
  </configSections>
</configuration>


--=-25FBwe9m5oyQx7t2vtCy
Content-Disposition: attachment; filename=switch-test.cs
Content-Type: text/plain; name=switch-test.cs; charset=UTF-8
Content-Transfer-Encoding: 7bit

// Test switches...

using System;
using System.Diagnostics;
using System.Configuration;
using System.Collections;

public class SwitchTest {
  private static BooleanSwitch bs = new BooleanSwitch ("bool-switch", "This is a boolean switch");
  private static TraceSwitch ts = new TraceSwitch ("trace-switch", "This is a trace switch");

  private static void PrintDictionary (int indent, IDictionary d) {
    string s = new string (' ', indent);
    foreach (DictionaryEntry e in d) {
      Console.WriteLine ("{0}{1}={2}", s, e.Key, e.Value);
      IDictionary ed = e.Value as IDictionary;
      if (ed != null) {
        PrintDictionary (indent+1, ed);
      }
    }
  }

  private static void GetConfigSection (string s, int indent) {
    Console.WriteLine ("getting section: " + s);
    object o = ConfigurationSettings.GetConfig (s);
    if (o != null) {
      Console.WriteLine (" typeof(GetConfig(\"{0}\"))=", s, o);
      IDictionary d = o as IDictionary;
      if (d != null)
        PrintDictionary (indent, (IDictionary) o);
    }
    else {
      Console.WriteLine (" cannot open section: {0}", s);
    }
  }

  public static void Main () {
    // Test the switches...
    /*
    Type t = typeof (TextWriterTraceListener);
    Console.WriteLine ("type='{0}'", t.AssemblyQualifiedName);
     */
    Console.WriteLine ("bs level: " + bs.Enabled);
    Console.WriteLine ("ts level: " + ts.Level);

    Console.WriteLine ("-system.diagnostics configuration section-");
    GetConfigSection ("system.diagnostics", 0);

    Console.WriteLine ("-appSettings configuration section-");
    System.Collections.Specialized.NameValueCollection nvc = ConfigurationSettings.AppSettings;
    foreach (string s in nvc)
      Console.WriteLine ("{0}={1}", s, nvc[s]);

    Console.WriteLine ("-custom sections-");
    // this must be kept in sync with the .config file
    string[] sections = {"sample-section", "another-section", "group/section"};
    foreach (string s in sections) {
      GetConfigSection (s, 1);
    }
  }
}


--=-25FBwe9m5oyQx7t2vtCy--