H
Howard Swope
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<TKey,TValue>,
ICollection<KeyValuePair<TKey,TValue>>,
IEnumerable<KeyValuePair<TKey,TValue>>,
IDictionary,
ICollection,
IEnumerable,
ISerializable,
IDeserializationCallback
{
protected Dictionary<TKey, TValue> dictionary;
#region Constructors
public DictionaryBase()
{
dictionary = new Dictionary<TKey, TValue>();
}
public DictionaryBase(IDictionary<TKey,TValue> dictionary)
{
this.dictionary = new Dictionary<TKey,
TValue>(dictionary);
}
public DictionaryBase(IEqualityComparer<TKey> comparer)
{
dictionary = new Dictionary<TKey, TValue>(comparer);
}
public DictionaryBase(int capacity)
{
dictionary = new Dictionary<TKey, TValue>(capacity);
}
public DictionaryBase(IDictionary<TKey, TValue> dictionary,
IEqualityComparer<TKey> comparer)
{
dictionary = new Dictionary<TKey, TValue>(dictionary,
comparer);
}
public DictionaryBase(int capacity, IEqualityComparer<TKey>
comparer)
{
dictionary = new Dictionary<TKey, TValue>(capacity,
comparer);
}
#endregion
#region Properties
public virtual IEqualityComparer<TKey> Comparer
{
get { return dictionary.Comparer; }
}
public virtual int Count
{
get { return dictionary.Count; }
}
public virtual TValue this[TKey key]
{
get { return dictionary[key]; }
set { dictionary[key] = value; }
}
public virtual Dictionary<TKey,TValue>.KeyCollection Keys
{
get { return dictionary.Keys; }
}
public virtual Dictionary<TKey, TValue>.ValueCollection Values
{
get { return dictionary.Values;}
}
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.Clear();
}
public virtual bool ContainsKey(TKey key)
{
return dictionary.ContainsKey(key);
}
public virtual bool ContainsValue(TValue value)
{
return dictionary.ContainsValue(value);
}
public virtual bool Contains(KeyValuePair<TKey,TValue> item)
{
if (dictionary.ContainsKey(item.Key) &&
dictionary[item.Key].Equals(item.Value))
return true;
else
return false;
}
public override bool Equals(Object obj)
{
return dictionary.Equals(obj);
}
public virtual Dictionary<TKey,TValue>.Enumerator
GetEnumerator()
{
return dictionary.GetEnumerator();
}
public override int GetHashCode()
{
return dictionary.GetHashCode();
}
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
dictionary.GetObjectData(info, context);
}
public virtual void OnDeserialization(Object sender)
{
dictionary.OnDeserialization(sender);
}
public virtual bool Remove(TKey key)
{
return dictionary.Remove(key);
}
public virtual bool Remove(KeyValuePair<TKey,TValue> item)
{
if (dictionary.ContainsKey(item.Key) &&
dictionary[item.Key].Equals(item.Value))
{
dictionary.Remove(item.Key);
return true;
}
else
{
return false;
}
}
public override String ToString()
{
return dictionary.ToString();
}
public virtual bool TryGetValue(TKey key, out TValue value)
{
if (dictionary.ContainsKey(key))
{
value = this[key];
return true;
}
else
{
value = default(TValue);
return false;
}
}
public virtual void CopyTo(KeyValuePair<TKey,TValue>[] array,
int arrayIndex)
{
int count = 0;
foreach (KeyValuePair<TKey,TValue> item in dictionary)
{
array[arrayIndex + count] =
new
KeyValuePair<TKey,TValue>(item.Key,item.Value);
++count;
}
}
#endregion
#region IDictionary<TKey,TValue> Members
void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
{
this.Add(key,value);
}
bool IDictionary<TKey, TValue>.ContainsKey(TKey key)
{
return this.ContainsKey(key);
}
ICollection<TKey> IDictionary<TKey, TValue>.Keys
{
get { return this.Keys; }
}
bool IDictionary<TKey, TValue>.Remove(TKey key)
{
return this.Remove(key);
}
ICollection<TValue> IDictionary<TKey, TValue>.Values
{
get { return this.Values; }
}
TValue IDictionary<TKey, TValue>.this[TKey key]
{
get
{
return this[key];
}
set
{
this[key] = value;
}
}
#endregion
#region ICollection<KeyValuePair<TKey,TValue>> Members
void ICollection<KeyValuePair<TKey,
TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
this.Add(item.Key,item.Value);
}
void ICollection<KeyValuePair<TKey, TValue>>.Clear()
{
this.Clear();
}
bool ICollection<KeyValuePair<TKey,
TValue>>.Contains(KeyValuePair<TKey, TValue> item)
{
return this.Contains(item);
}
void ICollection<KeyValuePair<TKey,
TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
this.CopyTo(array,arrayIndex);
}
int ICollection<KeyValuePair<TKey, TValue>>.Count
{
get { return this.Count; }
}
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
{
get { return this.IsReadOnly; }
}
bool ICollection<KeyValuePair<TKey,
TValue>>.Remove(KeyValuePair<TKey, TValue> item)
{
return this.Remove(item);
}
#endregion
#region IEnumerable<KeyValuePair<TKey,TValue>> Members
IEnumerator<KeyValuePair<TKey, TValue>>
IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
#region IDictionary Members
void IDictionary.Add(object key, object value)
{
this.Add((TKey)key,(TValue)value);
}
void IDictionary.Clear()
{
this.Clear();
}
bool IDictionary.Contains(object key)
{
return this.ContainsKey((TKey)key);
}
IDictionaryEnumerator IDictionary.GetEnumerator()
{
return this.GetEnumerator();
}
bool IDictionary.IsFixedSize
{
get { return this.IsFixedSize; }
}
bool IDictionary.IsReadOnly
{
get { return this.IsReadOnly; }
}
ICollection IDictionary.Keys
{
get { return this.Keys; }
}
void IDictionary.Remove(object key)
{
this.Remove((TKey)key);
}
ICollection IDictionary.Values
{
get { return this.Values; }
}
object IDictionary.this[object key]
{
get
{
return this[(TKey)key];
}
set
{
this[(TKey)key] = (TValue)value;
}
}
#endregion
#region ICollection Members
void ICollection.CopyTo(Array array, int index)
{
this.CopyTo((KeyValuePair<TKey,TValue>[])array,index);
}
int ICollection.Count
{
get { return this.Count; }
}
bool ICollection.IsSynchronized
{
get { return this.IsSynchronized; }
}
object ICollection.SyncRoot
{
get { return this.SyncRoot; }
}
#endregion
#region ISerializable Members
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context)
{
this.GetObjectData(info,context);
}
#endregion
#region IDeserializationCallback Members
void IDeserializationCallback.OnDeserialization(object sender)
{
this.OnDeserialization(sender);
}
#endregion
}
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<TKey,TValue>,
ICollection<KeyValuePair<TKey,TValue>>,
IEnumerable<KeyValuePair<TKey,TValue>>,
IDictionary,
ICollection,
IEnumerable,
ISerializable,
IDeserializationCallback
{
protected Dictionary<TKey, TValue> dictionary;
#region Constructors
public DictionaryBase()
{
dictionary = new Dictionary<TKey, TValue>();
}
public DictionaryBase(IDictionary<TKey,TValue> dictionary)
{
this.dictionary = new Dictionary<TKey,
TValue>(dictionary);
}
public DictionaryBase(IEqualityComparer<TKey> comparer)
{
dictionary = new Dictionary<TKey, TValue>(comparer);
}
public DictionaryBase(int capacity)
{
dictionary = new Dictionary<TKey, TValue>(capacity);
}
public DictionaryBase(IDictionary<TKey, TValue> dictionary,
IEqualityComparer<TKey> comparer)
{
dictionary = new Dictionary<TKey, TValue>(dictionary,
comparer);
}
public DictionaryBase(int capacity, IEqualityComparer<TKey>
comparer)
{
dictionary = new Dictionary<TKey, TValue>(capacity,
comparer);
}
#endregion
#region Properties
public virtual IEqualityComparer<TKey> Comparer
{
get { return dictionary.Comparer; }
}
public virtual int Count
{
get { return dictionary.Count; }
}
public virtual TValue this[TKey key]
{
get { return dictionary[key]; }
set { dictionary[key] = value; }
}
public virtual Dictionary<TKey,TValue>.KeyCollection Keys
{
get { return dictionary.Keys; }
}
public virtual Dictionary<TKey, TValue>.ValueCollection Values
{
get { return dictionary.Values;}
}
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.Clear();
}
public virtual bool ContainsKey(TKey key)
{
return dictionary.ContainsKey(key);
}
public virtual bool ContainsValue(TValue value)
{
return dictionary.ContainsValue(value);
}
public virtual bool Contains(KeyValuePair<TKey,TValue> item)
{
if (dictionary.ContainsKey(item.Key) &&
dictionary[item.Key].Equals(item.Value))
return true;
else
return false;
}
public override bool Equals(Object obj)
{
return dictionary.Equals(obj);
}
public virtual Dictionary<TKey,TValue>.Enumerator
GetEnumerator()
{
return dictionary.GetEnumerator();
}
public override int GetHashCode()
{
return dictionary.GetHashCode();
}
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
dictionary.GetObjectData(info, context);
}
public virtual void OnDeserialization(Object sender)
{
dictionary.OnDeserialization(sender);
}
public virtual bool Remove(TKey key)
{
return dictionary.Remove(key);
}
public virtual bool Remove(KeyValuePair<TKey,TValue> item)
{
if (dictionary.ContainsKey(item.Key) &&
dictionary[item.Key].Equals(item.Value))
{
dictionary.Remove(item.Key);
return true;
}
else
{
return false;
}
}
public override String ToString()
{
return dictionary.ToString();
}
public virtual bool TryGetValue(TKey key, out TValue value)
{
if (dictionary.ContainsKey(key))
{
value = this[key];
return true;
}
else
{
value = default(TValue);
return false;
}
}
public virtual void CopyTo(KeyValuePair<TKey,TValue>[] array,
int arrayIndex)
{
int count = 0;
foreach (KeyValuePair<TKey,TValue> item in dictionary)
{
array[arrayIndex + count] =
new
KeyValuePair<TKey,TValue>(item.Key,item.Value);
++count;
}
}
#endregion
#region IDictionary<TKey,TValue> Members
void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
{
this.Add(key,value);
}
bool IDictionary<TKey, TValue>.ContainsKey(TKey key)
{
return this.ContainsKey(key);
}
ICollection<TKey> IDictionary<TKey, TValue>.Keys
{
get { return this.Keys; }
}
bool IDictionary<TKey, TValue>.Remove(TKey key)
{
return this.Remove(key);
}
ICollection<TValue> IDictionary<TKey, TValue>.Values
{
get { return this.Values; }
}
TValue IDictionary<TKey, TValue>.this[TKey key]
{
get
{
return this[key];
}
set
{
this[key] = value;
}
}
#endregion
#region ICollection<KeyValuePair<TKey,TValue>> Members
void ICollection<KeyValuePair<TKey,
TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
this.Add(item.Key,item.Value);
}
void ICollection<KeyValuePair<TKey, TValue>>.Clear()
{
this.Clear();
}
bool ICollection<KeyValuePair<TKey,
TValue>>.Contains(KeyValuePair<TKey, TValue> item)
{
return this.Contains(item);
}
void ICollection<KeyValuePair<TKey,
TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
this.CopyTo(array,arrayIndex);
}
int ICollection<KeyValuePair<TKey, TValue>>.Count
{
get { return this.Count; }
}
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
{
get { return this.IsReadOnly; }
}
bool ICollection<KeyValuePair<TKey,
TValue>>.Remove(KeyValuePair<TKey, TValue> item)
{
return this.Remove(item);
}
#endregion
#region IEnumerable<KeyValuePair<TKey,TValue>> Members
IEnumerator<KeyValuePair<TKey, TValue>>
IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
#region IDictionary Members
void IDictionary.Add(object key, object value)
{
this.Add((TKey)key,(TValue)value);
}
void IDictionary.Clear()
{
this.Clear();
}
bool IDictionary.Contains(object key)
{
return this.ContainsKey((TKey)key);
}
IDictionaryEnumerator IDictionary.GetEnumerator()
{
return this.GetEnumerator();
}
bool IDictionary.IsFixedSize
{
get { return this.IsFixedSize; }
}
bool IDictionary.IsReadOnly
{
get { return this.IsReadOnly; }
}
ICollection IDictionary.Keys
{
get { return this.Keys; }
}
void IDictionary.Remove(object key)
{
this.Remove((TKey)key);
}
ICollection IDictionary.Values
{
get { return this.Values; }
}
object IDictionary.this[object key]
{
get
{
return this[(TKey)key];
}
set
{
this[(TKey)key] = (TValue)value;
}
}
#endregion
#region ICollection Members
void ICollection.CopyTo(Array array, int index)
{
this.CopyTo((KeyValuePair<TKey,TValue>[])array,index);
}
int ICollection.Count
{
get { return this.Count; }
}
bool ICollection.IsSynchronized
{
get { return this.IsSynchronized; }
}
object ICollection.SyncRoot
{
get { return this.SyncRoot; }
}
#endregion
#region ISerializable Members
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context)
{
this.GetObjectData(info,context);
}
#endregion
#region IDeserializationCallback Members
void IDeserializationCallback.OnDeserialization(object sender)
{
this.OnDeserialization(sender);
}
#endregion
}