This problem has been bugging me for a while. I have created a collection
class and implemented it in a C# library. If I inherit from this class in
another C# assembly and it works, but if I inherit from this class in a C++
/ CLI assembly it won't compile.
This indicates a problem in the CLR. Can anyone shed some light on this. The
class follows:
public abstract class DictionaryBase< TKey,TValue:
IDictionary<TKe y,TValue>,
ICollection<Key ValuePair<TKey, TValue>>,
IEnumerable<Key ValuePair<TKey, TValue>>,
IDictionary,
ICollection,
IEnumerable,
ISerializable,
IDeserializatio nCallback
{
protected Dictionary<TKey , TValuedictionar y;
#region Constructors
public DictionaryBase( )
{
dictionary = new Dictionary<TKey , TValue>();
}
public DictionaryBase( IDictionary<TKe y,TValuediction ary)
{
this.dictionary = new Dictionary<TKey ,
TValue>(diction ary);
}
public DictionaryBase( IEqualityCompar er<TKeycomparer )
{
dictionary = new Dictionary<TKey , TValue>(compare r);
}
public DictionaryBase( int capacity)
{
dictionary = new Dictionary<TKey , TValue>(capacit y);
}
public DictionaryBase( IDictionary<TKe y, TValuedictionar y,
IEqualityCompar er<TKeycomparer )
{
dictionary = new Dictionary<TKey , TValue>(diction ary,
comparer);
}
public DictionaryBase( int capacity, IEqualityCompar er<TKey>
comparer)
{
dictionary = new Dictionary<TKey , TValue>(capacit y,
comparer);
}
#endregion
#region Properties
public virtual IEqualityCompar er<TKeyComparer
{
get { return dictionary.Comp arer; }
}
public virtual int Count
{
get { return dictionary.Coun t; }
}
public virtual TValue this[TKey key]
{
get { return dictionary[key]; }
set { dictionary[key] = value; }
}
public virtual Dictionary<TKey ,TValue>.KeyCol lection Keys
{
get { return dictionary.Keys ; }
}
public virtual Dictionary<TKey , TValue>.ValueCo llection Values
{
get { return dictionary.Valu es;}
}
public virtual bool IsReadOnly
{
get { return false; }
}
public virtual bool IsFixedSize
{
get { return false; }
}
public virtual bool IsSynchronized
{
get { return false; }
}
public virtual object SyncRoot
{
get { return null; }
}
#endregion
#region Methods
public virtual void Add(TKey key, TValue value)
{
dictionary.Add( key, value);
}
public virtual void Clear()
{
dictionary.Clea r();
}
public virtual bool ContainsKey(TKe y key)
{
return dictionary.Cont ainsKey(key);
}
public virtual bool ContainsValue(T Value value)
{
return dictionary.Cont ainsValue(value );
}
public virtual bool Contains(KeyVal uePair<TKey,TVa lueitem)
{
if (dictionary.Con tainsKey(item.K ey) &&
dictionary[item.Key].Equals(item.Va lue))
return true;
else
return false;
}
public override bool Equals(Object obj)
{
return dictionary.Equa ls(obj);
}
public virtual Dictionary<TKey ,TValue>.Enumer ator
GetEnumerator()
{
return dictionary.GetE numerator();
}
public override int GetHashCode()
{
return dictionary.GetH ashCode();
}
public virtual void GetObjectData(S erializationInf o info,
StreamingContex t context)
{
dictionary.GetO bjectData(info, context);
}
public virtual void OnDeserializati on(Object sender)
{
dictionary.OnDe serialization(s ender);
}
public virtual bool Remove(TKey key)
{
return dictionary.Remo ve(key);
}
public virtual bool Remove(KeyValue Pair<TKey,TValu eitem)
{
if (dictionary.Con tainsKey(item.K ey) &&
dictionary[item.Key].Equals(item.Va lue))
{
dictionary.Remo ve(item.Key);
return true;
}
else
{
return false;
}
}
public override String ToString()
{
return dictionary.ToSt ring();
}
public virtual bool TryGetValue(TKe y key, out TValue value)
{
if (dictionary.Con tainsKey(key))
{
value = this[key];
return true;
}
else
{
value = default(TValue) ;
return false;
}
}
public virtual void CopyTo(KeyValue Pair<TKey,TValu e>[] array,
int arrayIndex)
{
int count = 0;
foreach (KeyValuePair<T Key,TValueitem in dictionary)
{
array[arrayIndex + count] =
new
KeyValuePair<TK ey,TValue>(item .Key,item.Value );
++count;
}
}
#endregion
#region IDictionary<TKe y,TValueMembers
void IDictionary<TKe y, TValue>.Add(TKe y key, TValue value)
{
this.Add(key,va lue);
}
bool IDictionary<TKe y, TValue>.Contain sKey(TKey key)
{
return this.ContainsKe y(key);
}
ICollection<TKe yIDictionary<TK ey, TValue>.Keys
{
get { return this.Keys; }
}
bool IDictionary<TKe y, TValue>.Remove( TKey key)
{
return this.Remove(key );
}
ICollection<TVa lueIDictionary< TKey, TValue>.Values
{
get { return this.Values; }
}
TValue IDictionary<TKe y, TValue>.this[TKey key]
{
get
{
return this[key];
}
set
{
this[key] = value;
}
}
#endregion
#region ICollection<Key ValuePair<TKey, TValue>Members
void ICollection<Key ValuePair<TKey,
TValue>>.Add(Ke yValuePair<TKey , TValueitem)
{
this.Add(item.K ey,item.Value);
}
void ICollection<Key ValuePair<TKey, TValue>>.Clear( )
{
this.Clear();
}
bool ICollection<Key ValuePair<TKey,
TValue>>.Contai ns(KeyValuePair <TKey, TValueitem)
{
return this.Contains(i tem);
}
void ICollection<Key ValuePair<TKey,
TValue>>.CopyTo (KeyValuePair<T Key, TValue>[] array, int arrayIndex)
{
this.CopyTo(arr ay,arrayIndex);
}
int ICollection<Key ValuePair<TKey, TValue>>.Count
{
get { return this.Count; }
}
bool ICollection<Key ValuePair<TKey, TValue>>.IsRead Only
{
get { return this.IsReadOnly ; }
}
bool ICollection<Key ValuePair<TKey,
TValue>>.Remove (KeyValuePair<T Key, TValueitem)
{
return this.Remove(ite m);
}
#endregion
#region IEnumerable<Key ValuePair<TKey, TValue>Members
IEnumerator<Key ValuePair<TKey, TValue>>
IEnumerable<Key ValuePair<TKey, TValue>>.GetEnu merator()
{
return this.GetEnumera tor();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.Get Enumerator()
{
return this.GetEnumera tor();
}
#endregion
#region IDictionary Members
void IDictionary.Add (object key, object value)
{
this.Add((TKey) key,(TValue)val ue);
}
void IDictionary.Cle ar()
{
this.Clear();
}
bool IDictionary.Con tains(object key)
{
return this.ContainsKe y((TKey)key);
}
IDictionaryEnum erator IDictionary.Get Enumerator()
{
return this.GetEnumera tor();
}
bool IDictionary.IsF ixedSize
{
get { return this.IsFixedSiz e; }
}
bool IDictionary.IsR eadOnly
{
get { return this.IsReadOnly ; }
}
ICollection IDictionary.Key s
{
get { return this.Keys; }
}
void IDictionary.Rem ove(object key)
{
this.Remove((TK ey)key);
}
ICollection IDictionary.Val ues
{
get { return this.Values; }
}
object IDictionary.thi s[object key]
{
get
{
return this[(TKey)key];
}
set
{
this[(TKey)key] = (TValue)value;
}
}
#endregion
#region ICollection Members
void ICollection.Cop yTo(Array array, int index)
{
this.CopyTo((Ke yValuePair<TKey ,TValue>[])array,index);
}
int ICollection.Cou nt
{
get { return this.Count; }
}
bool ICollection.IsS ynchronized
{
get { return this.IsSynchron ized; }
}
object ICollection.Syn cRoot
{
get { return this.SyncRoot; }
}
#endregion
#region ISerializable Members
void ISerializable.G etObjectData(Se rializationInfo info,
StreamingContex t context)
{
this.GetObjectD ata(info,contex t);
}
#endregion
#region IDeserializatio nCallback Members
void IDeserializatio nCallback.OnDes erialization(ob ject sender)
{
this.OnDeserial ization(sender) ;
}
#endregion
}
--
Howard Swope [ mailto:howard.s wopeATnavteqDOT com ]
Technical Lead
Media Development
Navteq Traffic [ http://www.navteq.com ] [ http://www.traffic.com ]
class and implemented it in a C# library. If I inherit from this class in
another C# assembly and it works, but if I inherit from this class in a C++
/ CLI assembly it won't compile.
This indicates a problem in the CLR. Can anyone shed some light on this. The
class follows:
public abstract class DictionaryBase< TKey,TValue:
IDictionary<TKe y,TValue>,
ICollection<Key ValuePair<TKey, TValue>>,
IEnumerable<Key ValuePair<TKey, TValue>>,
IDictionary,
ICollection,
IEnumerable,
ISerializable,
IDeserializatio nCallback
{
protected Dictionary<TKey , TValuedictionar y;
#region Constructors
public DictionaryBase( )
{
dictionary = new Dictionary<TKey , TValue>();
}
public DictionaryBase( IDictionary<TKe y,TValuediction ary)
{
this.dictionary = new Dictionary<TKey ,
TValue>(diction ary);
}
public DictionaryBase( IEqualityCompar er<TKeycomparer )
{
dictionary = new Dictionary<TKey , TValue>(compare r);
}
public DictionaryBase( int capacity)
{
dictionary = new Dictionary<TKey , TValue>(capacit y);
}
public DictionaryBase( IDictionary<TKe y, TValuedictionar y,
IEqualityCompar er<TKeycomparer )
{
dictionary = new Dictionary<TKey , TValue>(diction ary,
comparer);
}
public DictionaryBase( int capacity, IEqualityCompar er<TKey>
comparer)
{
dictionary = new Dictionary<TKey , TValue>(capacit y,
comparer);
}
#endregion
#region Properties
public virtual IEqualityCompar er<TKeyComparer
{
get { return dictionary.Comp arer; }
}
public virtual int Count
{
get { return dictionary.Coun t; }
}
public virtual TValue this[TKey key]
{
get { return dictionary[key]; }
set { dictionary[key] = value; }
}
public virtual Dictionary<TKey ,TValue>.KeyCol lection Keys
{
get { return dictionary.Keys ; }
}
public virtual Dictionary<TKey , TValue>.ValueCo llection Values
{
get { return dictionary.Valu es;}
}
public virtual bool IsReadOnly
{
get { return false; }
}
public virtual bool IsFixedSize
{
get { return false; }
}
public virtual bool IsSynchronized
{
get { return false; }
}
public virtual object SyncRoot
{
get { return null; }
}
#endregion
#region Methods
public virtual void Add(TKey key, TValue value)
{
dictionary.Add( key, value);
}
public virtual void Clear()
{
dictionary.Clea r();
}
public virtual bool ContainsKey(TKe y key)
{
return dictionary.Cont ainsKey(key);
}
public virtual bool ContainsValue(T Value value)
{
return dictionary.Cont ainsValue(value );
}
public virtual bool Contains(KeyVal uePair<TKey,TVa lueitem)
{
if (dictionary.Con tainsKey(item.K ey) &&
dictionary[item.Key].Equals(item.Va lue))
return true;
else
return false;
}
public override bool Equals(Object obj)
{
return dictionary.Equa ls(obj);
}
public virtual Dictionary<TKey ,TValue>.Enumer ator
GetEnumerator()
{
return dictionary.GetE numerator();
}
public override int GetHashCode()
{
return dictionary.GetH ashCode();
}
public virtual void GetObjectData(S erializationInf o info,
StreamingContex t context)
{
dictionary.GetO bjectData(info, context);
}
public virtual void OnDeserializati on(Object sender)
{
dictionary.OnDe serialization(s ender);
}
public virtual bool Remove(TKey key)
{
return dictionary.Remo ve(key);
}
public virtual bool Remove(KeyValue Pair<TKey,TValu eitem)
{
if (dictionary.Con tainsKey(item.K ey) &&
dictionary[item.Key].Equals(item.Va lue))
{
dictionary.Remo ve(item.Key);
return true;
}
else
{
return false;
}
}
public override String ToString()
{
return dictionary.ToSt ring();
}
public virtual bool TryGetValue(TKe y key, out TValue value)
{
if (dictionary.Con tainsKey(key))
{
value = this[key];
return true;
}
else
{
value = default(TValue) ;
return false;
}
}
public virtual void CopyTo(KeyValue Pair<TKey,TValu e>[] array,
int arrayIndex)
{
int count = 0;
foreach (KeyValuePair<T Key,TValueitem in dictionary)
{
array[arrayIndex + count] =
new
KeyValuePair<TK ey,TValue>(item .Key,item.Value );
++count;
}
}
#endregion
#region IDictionary<TKe y,TValueMembers
void IDictionary<TKe y, TValue>.Add(TKe y key, TValue value)
{
this.Add(key,va lue);
}
bool IDictionary<TKe y, TValue>.Contain sKey(TKey key)
{
return this.ContainsKe y(key);
}
ICollection<TKe yIDictionary<TK ey, TValue>.Keys
{
get { return this.Keys; }
}
bool IDictionary<TKe y, TValue>.Remove( TKey key)
{
return this.Remove(key );
}
ICollection<TVa lueIDictionary< TKey, TValue>.Values
{
get { return this.Values; }
}
TValue IDictionary<TKe y, TValue>.this[TKey key]
{
get
{
return this[key];
}
set
{
this[key] = value;
}
}
#endregion
#region ICollection<Key ValuePair<TKey, TValue>Members
void ICollection<Key ValuePair<TKey,
TValue>>.Add(Ke yValuePair<TKey , TValueitem)
{
this.Add(item.K ey,item.Value);
}
void ICollection<Key ValuePair<TKey, TValue>>.Clear( )
{
this.Clear();
}
bool ICollection<Key ValuePair<TKey,
TValue>>.Contai ns(KeyValuePair <TKey, TValueitem)
{
return this.Contains(i tem);
}
void ICollection<Key ValuePair<TKey,
TValue>>.CopyTo (KeyValuePair<T Key, TValue>[] array, int arrayIndex)
{
this.CopyTo(arr ay,arrayIndex);
}
int ICollection<Key ValuePair<TKey, TValue>>.Count
{
get { return this.Count; }
}
bool ICollection<Key ValuePair<TKey, TValue>>.IsRead Only
{
get { return this.IsReadOnly ; }
}
bool ICollection<Key ValuePair<TKey,
TValue>>.Remove (KeyValuePair<T Key, TValueitem)
{
return this.Remove(ite m);
}
#endregion
#region IEnumerable<Key ValuePair<TKey, TValue>Members
IEnumerator<Key ValuePair<TKey, TValue>>
IEnumerable<Key ValuePair<TKey, TValue>>.GetEnu merator()
{
return this.GetEnumera tor();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.Get Enumerator()
{
return this.GetEnumera tor();
}
#endregion
#region IDictionary Members
void IDictionary.Add (object key, object value)
{
this.Add((TKey) key,(TValue)val ue);
}
void IDictionary.Cle ar()
{
this.Clear();
}
bool IDictionary.Con tains(object key)
{
return this.ContainsKe y((TKey)key);
}
IDictionaryEnum erator IDictionary.Get Enumerator()
{
return this.GetEnumera tor();
}
bool IDictionary.IsF ixedSize
{
get { return this.IsFixedSiz e; }
}
bool IDictionary.IsR eadOnly
{
get { return this.IsReadOnly ; }
}
ICollection IDictionary.Key s
{
get { return this.Keys; }
}
void IDictionary.Rem ove(object key)
{
this.Remove((TK ey)key);
}
ICollection IDictionary.Val ues
{
get { return this.Values; }
}
object IDictionary.thi s[object key]
{
get
{
return this[(TKey)key];
}
set
{
this[(TKey)key] = (TValue)value;
}
}
#endregion
#region ICollection Members
void ICollection.Cop yTo(Array array, int index)
{
this.CopyTo((Ke yValuePair<TKey ,TValue>[])array,index);
}
int ICollection.Cou nt
{
get { return this.Count; }
}
bool ICollection.IsS ynchronized
{
get { return this.IsSynchron ized; }
}
object ICollection.Syn cRoot
{
get { return this.SyncRoot; }
}
#endregion
#region ISerializable Members
void ISerializable.G etObjectData(Se rializationInfo info,
StreamingContex t context)
{
this.GetObjectD ata(info,contex t);
}
#endregion
#region IDeserializatio nCallback Members
void IDeserializatio nCallback.OnDes erialization(ob ject sender)
{
this.OnDeserial ization(sender) ;
}
#endregion
}
--
Howard Swope [ mailto:howard.s wopeATnavteqDOT com ]
Technical Lead
Media Development
Navteq Traffic [ http://www.navteq.com ] [ http://www.traffic.com ]
Comment