Search code examples
c#visual-studiodebuggingvarwatch

"var" debugging reveals unexisting "System.Configuration.KeyValueInternalCollection", what's going on?


I'm heavily struggling with the reading of C# configuration files, this is my latest failure:

I have all names of the sections in my configuration file, they are stored in a List<string>, called names. I would like to read its content, for which I do the following:

foreach (string section_name in names)
{
   var interface_settings = ConfigurationManager.GetSection(section_name);
   // do something with "interface_settings"
}

I have no idea what type interface_settings is, so I debug this piece of code, in order to see interface_settings in the watch-window, and indeed:
interface_settings is mentioned being of the type System.Configuration.KeyValueInternalCollection.

I update my source code accordingly:

foreach (string section_name in names)
{
   System.Configuration.KeyValueInternalCollection interface_settings = 
     ConfigurationManager.GetSection(section_name);
   // do something with "interface_settings"
}

This code is wrong: System.Configuration.KeyValueInternalCollection does not exist!

How is it possible that the Visual Studio watch-window shows a non-existing type?

(I believe I can find some post, explaining how to read configuration sections and section groups, this question is especially concerning the apparently erroneous Visual Studio watch-window behaviour.)

Edit

Apparently the class KeyValueInternalCollection exists in file KeyValueInternalCollection.cs, as mentioned in this URL.


Solution

  • Based on my test, I reproduced your problem. Also, I agreed with Sebastian Schumann's advice about using NamedValueCollection instead.

    Code:

    foreach (string item in list)
                {
                    NameValueCollection section =(NameValueCollection) ConfigurationManager.GetSection(item);
    
                }
    

    how can I know that a class is an internal class?

    Classes, records, and structs declared directly within a namespace (in other words, that aren't nested within other classes or structs) can be either public or internal. internal is the default if no access modifier is specified.

    It comes from the link Class, record, and struct accessibility

    In order to know the parent class, I used your URL ("referencesource.microsoft.com"), but how can I find the parent class simply using the code?

    You can use the reflection to get the class name.

     string type = interface_settings.GetType().BaseType.Name;
    

    enter image description here

    This third question is a combination of both: when I find the parent class, how can I know this one is not an internal one? :-)

    Based on my test, you could search the key word in microsoft docs and you will find the access modifiter.

    For example, the link NameValueCollection Class has the following define:

    public class NameValueCollection : System.Collections.Specialized.NameObjectCollectionBase
    

    We can know it is a public type.