ReadOnlyNameValueCollection (reading from ConfigurationManager.GetSection)

Good, therefore .....

<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 that test contains one key, foo .

But GetSection returns object , so we need cast:

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

Well, that 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?

Casting to System.Collections.Specialized.NameValueCollection causes the code to work, but I really don't understand why the error.

And a search on ReadOnlyNameValueCollection on MSDN shows that there is no documentation for this class at all. It does not seem to exist. However, I have an instance of this type in my code.

+6
source share
1 answer

System.Configuration.ReadOnlyNameValueCollection is the internal class for the assembly of System.dll. Therefore, you cannot reference it from your code. This comes from System.Collections.Specialized.NameValueCollection , however, so you can do this using translation.

+13
source

Source: https://habr.com/ru/post/888230/


All Articles