generics question

generics question

am 11.01.2008 20:05:04 von mmodrall

Hi...

I'm a little perplexed by some of the generics collection stuff.
Dictionary being the rough equivalent of Hashtable, I was
surprised to find there was no direct way to get to the Dictionary SyncRoot
to do a simple
lock(collection.SyncRoot) {}

I found a post somewhere saying that you had to cast the Dictionary to an
ICollection to get to the SyncRoot and do a lock.

In one case I replaced a member Hashtable with a Dictionary
and got the lock statements to work with the ((ICollection)Mytable).SyncRoot
cast, but now I've just run into this:

There's an old class we had that extended Hashtable. I changed it to extend
from Dictionary instead, but when I try to change
lock (this.SyncRoot)
to
lock ((ICollection)this).SyncRoot)
I get the compiler error
Using the generic type 'System.Collections.Generic.ICollection' requires
'1' type arguments C:\test\MyClass.cs

a) I'm not clear why generics need the extra layer to get to the SyncRoot

b) I'm guessing it work on the member collection because I have an instance
of known types created, but I'm not clear why it won't work extending a
generic with the generic types defined.

I suppose I could just add an object member to my extension to sync on, but
I'm just trying to understand why it's so awkward to get to the one that's
buried in there.

Thanks
Mark

Re: generics question

am 11.01.2008 22:41:21 von newsgroups_remove

Hi,

see http://blogs.msdn.com/bclteam/archive/2005/03/15/396399.aspx

Kind regards,
Henning Krause

"Mark" wrote in message
news:A00DE061-D5A0-4377-9580-C93F0E6D9656@microsoft.com...
> Hi...
>
> I'm a little perplexed by some of the generics collection stuff.
> Dictionary being the rough equivalent of Hashtable, I was
> surprised to find there was no direct way to get to the Dictionary
> SyncRoot
> to do a simple
> lock(collection.SyncRoot) {}
>
> I found a post somewhere saying that you had to cast the Dictionary to an
> ICollection to get to the SyncRoot and do a lock.
>
> In one case I replaced a member Hashtable with a Dictionary > MyClass>
> and got the lock statements to work with the
> ((ICollection)Mytable).SyncRoot
> cast, but now I've just run into this:
>
> There's an old class we had that extended Hashtable. I changed it to
> extend
> from Dictionary instead, but when I try to change
> lock (this.SyncRoot)
> to
> lock ((ICollection)this).SyncRoot)
> I get the compiler error
> Using the generic type 'System.Collections.Generic.ICollection'
> requires
> '1' type arguments C:\test\MyClass.cs
>
> a) I'm not clear why generics need the extra layer to get to the SyncRoot
>
> b) I'm guessing it work on the member collection because I have an
> instance
> of known types created, but I'm not clear why it won't work extending a
> generic with the generic types defined.
>
> I suppose I could just add an object member to my extension to sync on,
> but
> I'm just trying to understand why it's so awkward to get to the one that's
> buried in there.
>
> Thanks
> Mark
>