[Mono-list] Collections of generic objects: IDictionary casting problem
Hi all I have a problem casting collections of generic objects. I've implemented two collections using the IDictionaryK,V generic interface; the values of these two collections are typed using classes that have a common generic ancestor (WrapperInnerType). I tried to cast both of them using such common denominator (WrapperInnerType), but I got both runtime and compiletime exceptions (see below). Probably I missed some subtlety of the semantics of generics constructs, but I'd like your opinion. Many Thanks! These are the available classes: class OuterTypes1 : IDictionaryString,OuterType1{...} class OuterType1 : WrapperInnerSubType1{...} class InnerSubType1 : InnerType{...} class OuterTypes2 : IDictionaryString,OuterType2{...} class OuterType2 : WrapperInnerSubType2{...} class InnerSubType2 : InnerType{...} class WrapperT where T : InnerType{...} class InnerType{...} This is the inheritance hierarchy: InnerType +-InnerSubType1 +-InnerSubType2 WrapperInnerType +-OuterTypes1 +-OuterTypes2 Here it is a sample excerpt from my code: { OuterTypes1 myOuterTypes1; OuterTypes2 myOuterTypes2; ... { IDictionaryString,OuterType1 types; /* This assignment works fine. */ types = (IDictionaryString,OuterType1)myOuterTypes1; } { IDictionaryString,WrapperInnerType types; /* The following assignment throws a compile-time exception: error CS0266: Cannot implicitly convert type 'System.Collections.Generic.IDictionarySystem.String,OuterType1' to 'System.Collections.Generic.IDictionarySystem.String,WrapperInnerType'. An explicit conversion exists (are you missing a cast?) but I can't figure out why, as WrapperInnerType should be a super type of OuterType1 (which extends WrapperInnerSubType1), as InnerType is a super type of InnerSubType1. */ types = (IDictionaryString,OuterType1)myOuterTypes1; } { IDictionaryString,WrapperInnerType types; /* The following assignment throws a run-time exception: System.InvalidCastException: Cannot cast from source type to destination type but I can't figure out why, as WrapperInnerType should be a super type of OuterType1 (which extends WrapperInnerSubType1), as InnerType is a super type of InnerSubType1. */ types = (IDictionaryString,WrapperInnerType)myOuterTypes1; } } ___ Mono-list maillist - Mono-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-list
Re: [Mono-list] [System.IO.Compression] OutOfMemory exception on DeflateStream instantiation
On 12/07/2006 12:54 PM, Andreia Gaita wrote: On 12/5/06, Generic 2006 [EMAIL PROTECTED] wrote: Someone suggests this is a misleading exception indicating that the zlib version on the system is too old (1.2.0.4), but I have verified that its version on my system (Debian sarge stable) is OK (1.2.2). Do you have any suggestion, please? Did you compile Mono, or installed the package? Which version of Mono do you have currently? andreia gaita Mono Team ___ Mono-list maillist - Mono-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-list 1.1.16 (package). I know I'm not so updated, but anyway I couldn't find any bugfix related to such an issue ([1] bug status seems still currently open). [1] http://bugzilla.ximian.com/show_bug.cgi?id=79665 ___ Mono-list maillist - Mono-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-list
[Mono-list] [System.IO.Compression] OutOfMemory exception on DeflateStream instantiation
Hi all I'm crashing against an annoying issue, regarding the instantiation of DeflateStream objects. Here it is my simple code: MemoryStream outputStream = new MemoryStream(); DeflateStream outputFilter = new DeflateStream( outputStream, CompressionMode.Compress, true ); // OutOfMemory exception is thrown here. I found a couple of bug reports about this [1], and I have also inspected DeflateStream source code, but apparently without success. The exception is thrown after an invocation to the underlying zlib: this.z_stream = create_z_stream (mode, gzip); if (z_stream == IntPtr.Zero) { throw new OutOfMemoryException (); } Someone suggests this is a misleading exception indicating that the zlib version on the system is too old (1.2.0.4), but I have verified that its version on my system (Debian sarge stable) is OK (1.2.2). Do you have any suggestion, please? [1] http://bugzilla.ximian.com/show_bug.cgi?id=79665 ___ Mono-list maillist - Mono-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-list
[Mono-list] [GTK#] Debian stable problem
Hi all Is there anybody using Mono on Debian stable (Sarge)? I'm experiencing a tough problem trying GTK# 1.0: it requires libpangocairo, but Debian stable DOESN'T ship with it (it's available on testing and unstable only)! How did you solved such an issue? Many thanks (I'm desperate...) ___ Mono-list maillist - Mono-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-list
[Mono-dev] Key matching issue in generic Dictionary
Hi all I'm facing a strange behavior affecting System.Collections.Generic.DictionaryTKey,TValue: when I try to search for a key among the dictionary entries using interface members such as ContainsKey(TKey key) or this[TKey key] I receive wrong responses (the key can't be find, whilst it's really there!). I supposed that it could be a problem on the TKey side (where I implemented System.IEquatableTKey), so I put some Console.WriteLine() inside the code, with the following result. A System.Collections.Generic.Dictionary instance is incapsulated in MyDictionary (an implementation of generic IDictionary) , TKey is assigned to Name type, TValue is irrelevant. Name has this constructor: Name(string). class MyDictionary { public bool ContainsKey(Name key) { Console.WriteLine(PdfDictionary.ContainsKey: key = + key.Value); return this.entries.ContainsKey(key); } } 1) I populate MyDictionary with 3 entry pairs, whose keys are: Name(Info), Name(Root), Name(Size); 2) I try to find Name(Encrypt) among MyDictionary keys, using MyDictionary.ContainsKey(Name key) and obtaining such console output: MyDictionary.ContainsKey: key = Encrypt // entered the MyDictionary.ContainsKey(Name key) method searching Name(Encrypt); // Name.Equals: this: Encrypt other: Info Name.Equals: this: Encrypt other: Root PdfDictionary[]: key = Size PdfDictionary.ContainsKey: key = Root PdfName.Equals: this: Root other: Size PdfDictionary[]: contains key = Size: False PdfDictionary[]:exception: The given key was not present in the dictionary. PdfDictionary[]: value = null; key = Size PdfName.Equals: this: Size other: Info PdfDictionary[]: pair: key = Info equals : False hashcode: -26085888 PdfName.Equals: this: Size other: Root PdfDictionary[]: pair: key = Root equals : False hashcode: 904636160 PdfName.Equals: this: Size other: Size PdfDictionary[]: pair: key = Size equals : True hashcode: -1994248064 ReadXRefTable:1.1: trailer count:3 ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list
[Mono-dev] [Fwd: Key matching issue in generic Dictionary] Error
Sorry, I erroneously sent an incomplete message! Original Message Subject:Key matching issue in generic Dictionary Date: Wed, 23 Aug 2006 15:04:02 +0200 From: Generic 2006 [EMAIL PROTECTED] To: mono-devel-list@lists.ximian.com Hi all I'm facing a strange behavior affecting System.Collections.Generic.DictionaryTKey,TValue: when I try to search for a key among the dictionary entries using interface members such as ContainsKey(TKey key) or this[TKey key] I receive wrong responses (the key can't be find, whilst it's really there!). I supposed that it could be a problem on the TKey side (where I implemented System.IEquatableTKey), so I put some Console.WriteLine() inside the code, with the following result. A System.Collections.Generic.Dictionary instance is incapsulated in MyDictionary (an implementation of generic IDictionary) , TKey is assigned to Name type, TValue is irrelevant. Name has this constructor: Name(string). class MyDictionary { public bool ContainsKey(Name key) { Console.WriteLine(PdfDictionary.ContainsKey: key = + key.Value); return this.entries.ContainsKey(key); } } 1) I populate MyDictionary with 3 entry pairs, whose keys are: Name(Info), Name(Root), Name(Size); 2) I try to find Name(Encrypt) among MyDictionary keys, using MyDictionary.ContainsKey(Name key) and obtaining such console output: MyDictionary.ContainsKey: key = Encrypt // entered the MyDictionary.ContainsKey(Name key) method searching Name(Encrypt); // Name.Equals: this: Encrypt other: Info Name.Equals: this: Encrypt other: Root PdfDictionary[]: key = Size PdfDictionary.ContainsKey: key = Root PdfName.Equals: this: Root other: Size PdfDictionary[]: contains key = Size: False PdfDictionary[]:exception: The given key was not present in the dictionary. PdfDictionary[]: value = null; key = Size PdfName.Equals: this: Size other: Info PdfDictionary[]: pair: key = Info equals : False hashcode: -26085888 PdfName.Equals: this: Size other: Root PdfDictionary[]: pair: key = Root equals : False hashcode: 904636160 PdfName.Equals: this: Size other: Size PdfDictionary[]: pair: key = Size equals : True hashcode: -1994248064 ReadXRefTable:1.1: trailer count:3 ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list
[Mono-dev] Key matching issue in generic Dictionary [correct message]
Hi all I'm facing a strange behavior affecting System.Collections.Generic.DictionaryTKey,TValue: when I try to search for a key among the dictionary entries using interface members such as ContainsKey(TKey key) or this[TKey key] I receive wrong responses (the key can't be find, whilst it's really there!). I supposed that it could be a problem on the TKey side (where I implemented System.IEquatableTKey), so I put some Console.WriteLine() inside the code to verify. A System.Collections.Generic.Dictionary instance is incapsulated in MyDictionary (an implementation of generic IDictionary), TKey is assigned to Name type, TValue is irrelevant. class Name : IEquatableName { public Name(string value) { ... } public bool Equals(Name obj) { Console.WriteLine(Name.Equals(): this: + this.value + other: + obj.Value); return this.value.Equals(obj.Value); } public string Value { get{return this.value;} } } class MyDictionary : IDictionaryName,Object { protected DictionaryName,Object entries = new DictionaryName,Object(); ... public bool ContainsKey(Name key) { Console.WriteLine(MyDictionary.ContainsKey: key = + key.Value); return this.entries.ContainsKey(key); } public Object this[Name key] { get { try { Console.WriteLine(Dictionary[]: key = + key.Value); return ((Object) this.entries[key]); } catch(Exception e) { Console.WriteLine(Dictionary[]:exception: + e.Message); foreach(KeyValuePairPdfName,Object kv in this.entries) { Console.WriteLine(PdfDictionary[]: pair: key = + kv.Key.Value + equals : + key.Equals(kv.Key)); } return null; } } } ... } Here's the test I made: 1) populate MyDictionary with 3 entry pairs, whose keys are: Name(Info), Name(Root), Name(Size); 2) try to find Name(Encrypt) among MyDictionary keys, invoking MyDictionary.ContainsKey(Name key) method and obtaining such console output: MyDictionary.ContainsKey: key = Encrypt Name.Equals(): this: Encrypt other: Info Name.Equals(): this: Encrypt other: Root 3) as you can see from the previous step, it strangely missed to invoke Name.Equals for the Name(Size) instance (just considered Name(Info) and Name(Root): why? 4) try to get the object related to the Name(Size) key invoking MyDictionary.this[Name key] method and obtaining such console output: Dictionary[]: key = Size Dictionary[]:exception: The given key was not present in the dictionary. Name.Equals(): this: Size other: Info Dictionary[]: pair: key = Info equals : False Name.Equals(): this: Size other: Root Dictionary[]: pair: key = Root equals : False Name.Equals(): this: Size other: Size Dictionary[]: pair: key = Size equals : True 5) as you can see from the previous step, before the exception message there is NO invocation to Name.Equals() method, despite the invocation to this.entries[key] which should trigger the equality comparison. Then the iteration through all the keys yelds the expected result (Name(Size) key exists and is positively equated -- so the problem is NOT in IEquatableName implementation!). So, the problem seems to be that System.Collections.Generic.DictionaryTKey,TValue has a faulty way to access its keys... What do you suggest about it? Is it a reported bug? How can I obtain the correct behavior? Many thanks ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list
Re: [Mono-dev] Key matching issue in generic Dictionary [correct message]
On 08/23/2006 04:35 PM, Brian Crowell wrote: Generic 2006 wrote: I'm facing a strange behavior affecting System.Collections.Generic.DictionaryTKey,TValue: when I try to search for a key among the dictionary entries using interface members such as ContainsKey(TKey key) or this[TKey key] I receive wrong responses (the key can't be find, whilst it's really there!). I supposed that it could be a problem on the TKey side (where I implemented System.IEquatableTKey), so I put some Console.WriteLine() inside the code to verify. A System.Collections.Generic.Dictionary instance is incapsulated in MyDictionary (an implementation of generic IDictionary), TKey is assigned to Name type, TValue is irrelevant. IEquatableT is not enough. You need to override Object.GetHashCode() and Object.Equals(). IEquatableT is optional, GetHashCode and Equals are mandatory. --Brian Thank you Brian for your prompt reply: I've just got an analogous hint from the guys at IRC #mono. So I should write something like this (please, let me know): class Name : IEquatableName { public Name(string value) { ... } // Mandatory!!! public override int GetHashCode() { return this.value.GetHashCode(); } // Mandatory!!! public override bool Equals(object obj) { if(obj is Name) return Equals((Name)obj); else return false; } public bool Equals(Name obj) { Console.WriteLine(Name.Equals(): this: + this.value + other: + obj.Value); return this.value.Equals(obj.Value); } public string Value { get{return this.value;} } } ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list