Missing ReadOnlyDictionary<>?

G

Guest

So did the .NET 2.0 working group just run out of steam before it got to a
ReadOnlyDictionary<> wrapper?

I am publishing events containing a SortedList<>, and obviously I don't want
event handlers to be able to do anything to the collection at the same time
other recipients could be handling it.

I found a ReadOnlyCollection<> wrapper, which I can apply to the .Keys and
..Values of the SortedList, but I can't find any way to wrap the SortedList
itself. I need to preserve SortedList functionality, but I would prefer to
have a programmatic way of ensuring recipients don't manhandle the shared
SortedList.

Is there any other solution to that short of writing my own
ReadOnlyDictionary, or cloning the collection for every recipient (which is
prohibitively expensive for me)?
 
B

Barry Kelly

Dave Booker said:
So did the .NET 2.0 working group just run out of steam before it got to a
ReadOnlyDictionary<> wrapper?

Dictionary<,> defines no virtual methods for speed reasons, so it's not
possible to create a ReadOnlyDictionary<,> which is replaceable with
Dictionary<,>. The best solution is to write one which implements
IDictionary<,>.

Personally, I think it would have been nice for them to provide a
read-only IDictionary<,> wrapper, but they didn't.

The rationale seems to be that Dictionary<,> and List<> are classes
useful for class implementation, but shouldn't be exposed to the outer
world, because they have too much public functionality, and so might
confuse inexperienced users. Apparently, the recommended way to expose
these as properties is via Collection said:
Is there any other solution to that short of writing my own
ReadOnlyDictionary, or cloning the collection for every recipient (which is
prohibitively expensive for me)?

I'd write a ReadOnlyDictionary wrapper, have it implement the same
interfaces as Dictionary<,>, and delegate non-mutable methods to the
underlying Dictionary<,>. Have the mutable operations throw
InvalidOperationException or something. Shouldn't take more than 5
minutes.

-- Barry
 
P

Peter Huang [MSFT]

Dear Customer,

Thanks for your great feedback that does to be a good idea.
I do understand your scenario, I highly suggest you can submit this
feedback to our product feedback center:
http://lab.msdn.microsoft.com/productfeedback/default.aspx

Also based on my research, here are some links for your reference.

A read-only IDictionary wrapper for .Net 1/1.1
http://www.dotnetguru2.org/index.php?p=121&more=1&c=1&tb=1&pb=1

http://www.koders.com/csharp/fidD6173E9CA777A0CB3BDF5B4F77F6DDFC835C6E6B.asp
x

Thanks for your understanding!

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Dear Customer,

Thanks for your mention.
I think our product team will evaluate the feedback.

Thanks for your understanding!

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top