5

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)?

Sebastian Schumann
  • 3,204
  • 19
  • 37

1 Answers1

5

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Sorry we can't upgrade to .Net 4.5 because we're using Visual Studio 2010. Ignoring the problem isn't an option because intellisence doesn't work and coding without intellisense hurts. – Sebastian Schumann Jun 04 '14 at 15:24
  • @Verarind: Use Visual Studio Intellisense instead of R#? Or just use R# 7.1, if you're saying that works? Alternatively, just give the type a different name - in fact, thinking about it, have you created your own type in `System.Collections.ObjectModel`? That's definitely a bad idea. – Jon Skeet Jun 04 '14 at 15:29
  • We can't use Resharper 7.1 on any computer without extra costs. I created a ReadOnlyDictionary in a different namespace but it was a straight forward port from .Net 4.5 having the same name. After an upgrade to 4.5 I want to remove my class and without any changes (except namespace adaptions) the exisiting code should work. I suggested to uninstall .Net Framework 4.5 but I'm not sure which problems this causes. Maybe there's any software installed that needs it. I'll rename my ReadOnlyDictionary. Thx. – Sebastian Schumann Jun 05 '14 at 05:12