Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .Net 4.0 mscorlib contains ReadOnlyDictionary?

I need a ReadOnlyDictionary. All posts told me that there is none in .Net 4.0. For that reason I created my own implementation.

After doing that Resharper 5.1 told me about an ambiguous reference to System.Collections.ObjectsModel.ReadOnlyDictionary but the code is compiling without errors.

I tried to use this implementation but it's not accessible. Why?

I wrote the following Code to test whether there is a ReadOnlyDictionary in .Net 4.0:

var foo = from assembly in AppDomain.CurrentDomain.GetAssemblies()
          where assembly.FullName.Contains("mscorlib")
          from type in assembly.GetTypes()
          where type.Name.StartsWith("ReadOnly")
          select type.FullName + " " + assembly.FullName;

And the surprising result is:

System.Collections.ReadOnlyCollectionBase mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Collections.ObjectModel.ReadOnlyCollection`1 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Collections.ObjectModel.ReadOnlyDictionary`2 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Collections.ObjectModel.ReadOnlyDictionaryHelpers mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Runtime.InteropServices.WindowsRuntime.ReadOnlyArrayAttribute mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Runtime.InteropServices.WindowsRuntime.ReadOnlyDictionaryKeyCollection`2 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Runtime.InteropServices.WindowsRuntime.ReadOnlyDictionaryKeyEnumerator`2 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Runtime.InteropServices.WindowsRuntime.ReadOnlyDictionaryValueCollection`2 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Runtime.InteropServices.WindowsRuntime.ReadOnlyDictionaryValueEnumerator`2 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Security.ReadOnlyPermissionSet mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Security.ReadOnlyPermissionSetEnumerator mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Collections.ArrayList+ReadOnlyList mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Collections.ArrayList+ReadOnlyArrayList mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

There is a ReadOnlyDictionary in .Net 4.0. I used dotPeek 1.1 to reflect for this type and it’s public.

Why is this type inaccessible? What can I do to prevent the ambiguous reference in Resharper 5.1 (7.1 doesn’t have this problem)?

like image 753
Sebastian Schumann Avatar asked Jun 04 '14 15:06

Sebastian Schumann


1 Answers

ReadOnlyDictionary was introduced in .NET 4.5. Now .NET 4.5 is an "over the top" install, which is why you're seeing it in your version of mscorlib. However, if your project is targeting .NET 4.0, then presumably Visual Studio has some reference assembly somewhere which tells it which types were really in .NET 4.0.

If you can upgrade your app to target .NET 4.5 instead, that's going to be the simplest fix. In terms of fixing the R# warning - you might want to just ignore it, if it only happens in an old version. If this is an open source project, you might want to document it somewhere.

like image 114
Jon Skeet Avatar answered Sep 30 '22 01:09

Jon Skeet