Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReadOnlyNameValueCollection (reading from ConfigurationManager.GetSection)

Ok, so.....

<section name="test" type="System.Configuration.NameValueFileSectionHandler" />
<test>
   <add key="foo" value="bar" />
</test>

var test = ConfigurationManager.GetSection("test");

So far so good. The debugger shows test contains one key, foo.

But GetSection returns object, so we need a cast:

var type = test.GetType();
// FullName: System.Configuration.ReadOnlyNameValueCollection
// Assembly: System

Ok, this should be simple enough. So....

using System;

var test = ConfigurationManager
               .GetSection("test") as ReadOnlyNameValueCollection;

error!

The type or namespace ReadOnlyNameValueCollection does not exist in the namespace System.Configuration. Are you missing an assembly reference?

err... wtf?

A cast to System.Collections.Specialized.NameValueCollection gets the code working, but I don't really understand why the error.

And a search for ReadOnlyNameValueCollection on MSDN shows there is no documentation on this class at all. It doesn't seem to exist. Yet I have an instance of that type in my code.

like image 483
fearofawhackplanet Avatar asked May 16 '11 14:05

fearofawhackplanet


1 Answers

System.Configuration.ReadOnlyNameValueCollection is an internal class to the System.dll assembly. So you can't refer to it from your code. It derives from System.Collections.Specialized.NameValueCollection, though, so that's why you're able to do that with the cast.

like image 150
Tim Avatar answered Nov 15 '22 04:11

Tim