这适用于C#,但不适用于C ++ / CLI [英] This works from C#, but not C++ / CLI
问题描述
这个问题一直困扰着我。我创建了一个集合
类并在C#库中实现它。如果我在另一个C#程序集中从这个类继承了另一个C#程序集并且它可以工作,但是如果我在C ++
/ CLI程序集中从这个类继承它就不会编译。
这表明CLR存在问题。任何人都可以对此有所了解。
类如下:
公共抽象类DictionaryBase< TKey,TValue:
IDictionary< TKey,TValue>,
ICollection< KeyValuePair< TKey,TValue>>,
IEnumerable< KeyValuePair< TKey,TValue>>,
IDictionary,
ICollection,
IEnumerable,
ISerializable,
IDeserializationCallback
{
protected Dictionary< TKey,TValuedictionary;
#region构造函数
public DictionaryBase()
{
dictionary = new Dictionary< TKey,TValue>();
}
public DictionaryBase(IDictionary< TKey,TValuedictionary)
{
this.dictionary = new Dictionary< TKey,
TValue>(字典);
}
public DictionaryBase(IEqualityComparer< TKeycomparer)
{
dictionary = new Dictionary< TKey,TValue>(comparer);
}
public DictionaryBase(int capacity)
{
dictionary = new Dictionary< TKey,TValue>(capacity);
}
public DictionaryBase(IDictionary< TKey,TValuedictionary,
IEqualityComparer< TKeycomparer)
{
字典=新词典< TKey,TValue>(字典,
比较器);
}
public DictionaryBase(int capacity,IEqualityComparer< TKey>
comparer)
{
dictionary = new Dictionary< TKey,TValue>(capacity,
comparer);
}
#endregion
#region Properties
公共虚拟IEqualityComparer< TKeyComparer
{
get {return dictionary.Comparer; }
}
public virtual int Count
{
get {return dictionary.Count; }
}
公共虚拟TValue这个[TKey键]
{
get {return字典[键]。 }
set {dictionary [key] = value; }
}
公共虚拟词典< TKey,TValue> .KeyCollection键
{
得到{return dictionary.Keys; }
}
公共虚拟字典< TKey,TValue> .ValueCollection值
{
得到{return dictionary.Values;}
}
public virtual bool IsReadOnly
{
得到{return false; }
}
public virtual bool IsFixedSize
{
get {return false; }
}
public virtual bool IsSynchronized
{
get {return false; }
}
公共虚拟对象SyncRoot
{
get {return null; }
}
#endregion
#region方法
public virtual void Add(TKey key ,TValue值)
{
dictionary.Add(键,值);
}
public virtual void清除()
{
dictionary.Clear();
}
public virtual bool ContainsKey(TKey key)
{
返回dictionary.ContainsKey(key);
}
>
public virtual bool ContainsValue(TValue value)
{
返回dictionary.ContainsValue(value);
}
public virtual bool包含(KeyValuePair< TKey,TValueitem)
{
if(dictionary.ContainsKey(item.Key)&& ;
字典[item.Key] .Equals(item.Value))
返回true;
else
返回false;
}
公共覆盖bool Equals(Object obj)
{
return dictionary.Equals(obj);
}
public virtual Dictionary< TKey,TValue> .Enumerator
GetEnumerator()
{
return dictionary.GetEnumerator();
}
公共覆盖int GetHashCode()
{
返回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删除(TKey键)
{
返回dictionary.Remove(key);
}
public virtual bool Remove(KeyValuePair< TKey,TValueitem )
{
if(dictionary.ContainsKey(item.Key)&&
dictionary [item.Key] .Equals(item .Value))
{
dictionary.Remove(item.Key);
返回true;
}
其他
{
返回false;
}
}
公共覆盖字符串ToString()
{
返回字典.ToString ();
}
public virtual bool TryGetValue(TKey key,out TValue value)
{
if(dictionary.ContainsKey(key))
{
value = this [key];
return true;
}
else
{
value = default(TValue);
返回false;
}
}
public virtual void CopyTo(KeyValuePair< TKey,TValue> [] array,
int arrayIndex)
{
int count = 0;
foreach(KeyValuePair< TKey,TValueitem in dictionary)
{
array [arrayIndex + count] =
new
KeyValuePair< TKey,TValue>(item.Key,item。价值);
++数量;
}
}
#endregion
#region IDictionary< TKey,TValueMembers
void IDictionary< TKey,TValue> .Add(TKey key,TValue value)
{
this.Add(key,value);
}
bool IDictionary< TKey,TValue> .ContainsKey(TKey key)
{
返回this.ContainsKey(键);
}
ICollection< TKeyIDictionary< TKey,TValue> ; .Keys
{
get {return this.Keys; }
}
bool IDictionary< TKey,TValue>。删除(TKey键)
{
返回this.Remove(key);
}
ICollection< TValueIDictionary< TKey,TValue> .Values
{
得到{return this.Values; }
}
TValue IDictionary< TKey,TValue> .this [TKey key]
{
get
{
返回此[key];
}
set
{
这[key] =价值;
}
}
#endregion
#region ICollection< KeyValuePair< TKey,TValue>成员
void ICollection< KeyValuePair< TKey,
TValue>> .Add(KeyValuePair< TKey,TValueitem)
{
this.Add(item.Key,item.Value);
}
void ICollection< KeyValuePair< TKey,TValue>> .Clear()
{
this.Clear( };
}
bool ICollection< KeyValuePair< TKey,
TValue>> .Contains(KeyValuePair< TKey,TValueitem )
{
返回this.Contains(item);
}
void ICollection< KeyValuePair< TKey,
TValue>> .CopyTo(KeyValuePair< TKey,TV alue> [] 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,TValueitem)
{
返回this.Remove(item);
}
#endregion
#region IEnumerable< KeyValuePair< TKey,TValue>成员
IEnumerator< KeyValuePair< TKey,TValue>>
IEnumerable< KeyValuePair< TKey,TValue>> .GetEnumerator()
{
返回this.GetEnumerator();
}
#endregion
#region IEnumerable成员
IEnumerator IEnumerable.GetEnumerator()
{
返回this.GetEnumerator();
}
#endregion
>
#region IDictionary会员
void IDictionary.Add(对象键,对象值)
{
this .Add((TKey)键,(TValue)值);
}
void IDictionary.Clear()
{ >
this.Clear();
}
bool IDictionary.Contains(对象键)
{
返回this.ContainsKey((TKey)键);
}
IDictionaryEnumerator IDictionary.GetEnumerator()
{
返回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(对象键)
{
this.Remove ((TKey)key);
}
ICollection IDictionary.Values
{
get {return this.Values; }
}
对象IDictionary.this [对象键]
{
get
{
返回此[(TKey)键];
}
set
{
这个[(TKey)键] =(TValue)值;
}
}
#endregion
#region ICollection会员
void ICollection.CopyTo(数组数组,int索引)
{
this.CopyTo((KeyValuePair< TKey,TValue> [])数组,索引);
}
int ICollection.Count
{
get {return this.Count; } b / b $
bool ICollection.IsSynchronized
{
get {return this.IsSynchronized; }
}
对象ICollection.SyncRoot
{
get {return this.SyncRoot; }
}
#endregion
#region ISerializable会员
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context)
{
this.GetObjectData(info,context);
}
#endregion
#region IDeserializationCallback会员
无效IDeserializationCallback .OnDeserialization(对象发送者)
{
this.OnDeserialization(sender);
}
#endregion
}
-
Howard Swope [mailto:howard.swopeATnavteqDOTcom]
技术主管
媒体开发
Navteq流量[ http:// www.navteq.com ] [ http://www.traffic.com ]
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, TValuedictionary;
#region Constructors
public DictionaryBase()
{
dictionary = new Dictionary<TKey, TValue>();
}
public DictionaryBase(IDictionary<TKey,TValuedictionary)
{
this.dictionary = new Dictionary<TKey,
TValue>(dictionary);
}
public DictionaryBase(IEqualityComparer<TKeycomparer)
{
dictionary = new Dictionary<TKey, TValue>(comparer);
}
public DictionaryBase(int capacity)
{
dictionary = new Dictionary<TKey, TValue>(capacity);
}
public DictionaryBase(IDictionary<TKey, TValuedictionary,
IEqualityComparer<TKeycomparer)
{
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<TKeyComparer
{
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,TValueitem)
{
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,TValueitem)
{
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,TValueitem in dictionary)
{
array[arrayIndex + count] =
new
KeyValuePair<TKey,TValue>(item.Key,item.Value);
++count;
}
}
#endregion
#region IDictionary<TKey,TValueMembers
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<TKeyIDictionary<TKey, TValue>.Keys
{
get { return this.Keys; }
}
bool IDictionary<TKey, TValue>.Remove(TKey key)
{
return this.Remove(key);
}
ICollection<TValueIDictionary<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, TValueitem)
{
this.Add(item.Key,item.Value);
}
void ICollection<KeyValuePair<TKey, TValue>>.Clear()
{
this.Clear();
}
bool ICollection<KeyValuePair<TKey,
TValue>>.Contains(KeyValuePair<TKey, TValueitem)
{
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, TValueitem)
{
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
}
--
Howard Swope [ mailto:howard.swopeATnavteqDOTcom ]
Technical Lead
Media Development
Navteq Traffic [ http://www.navteq.com ] [ http://www.traffic.com ]
推荐答案
<" Howard Swope" < howard.swopeATnavteqDOTcom>写道:
<"Howard Swope" <howard.swopeATnavteqDOTcom>wrote:
这个问题一直困扰着我。我创建了一个集合
类并在C#库中实现它。如果我在另一个C#程序集中从这个类继承了另一个C#程序集并且它可以工作,但是如果我在C ++
/ CLI程序集中从这个类继承它就不会编译。
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.
你能提供更多的细节而不是它不会编译吗?错误消息
将是一个良好的开端 - 一些示例(失败)代码会更好。
绝对理想是减少你的
集合类中的代码量,直到你遇到最小的失败。
Could you give more details than "it won''t compile"? The error message
would be a good start - some sample (failing) code would be better. The
absolute ideal would be to cut down on the amount of code in your
collection class as well, until you''ve got a minimal failure.
这表示CLR中存在问题。
This indicates a problem in the CLR.
为什么?我以为这会是C ++ / CLI编译器的问题
如果有的话。
-
Jon Skeet - < sk *** @ pobox.com>
网站: http://www.pobox.com/~skeet
博客: http://www.msmvps.com/jon.skeet
C#深度: http://csharpindepth.com
Why? I''d have thought it would be a problem with the C++/CLI compiler
if anything.
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Howard Swope写道:
Howard Swope wrote:
这个问题一直困扰着我。我创建了一个集合
类并在C#库中实现它。如果我在另一个C#程序集中从这个类继承了另一个C#程序集并且它可以工作,但是如果我在C ++
/ CLI程序集中从这个类继承它就不会编译。
这表明CLR存在问题。任何人都可以对此有所了解。
类如下:
[... snip ...]
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:
[... snip ...]
我没有任何麻烦编译:
ref class MyDictionary:public DictionaryBase< int,System :: String ^>
{
} ;
如果你可以在这里发布你的C ++ / CLI代码,并且你收到错误信息,我们可能会告诉你更多(尽管它会是我觉得在C#组中有点b / b
offtopic。
但有一件事 - 你确定你没有忘记参考在你的C ++ /
CLI类声明中?
I did not have any trouble compiling this:
ref class MyDictionary : public DictionaryBase<int, System::String^>
{
};
If you could post your C++/CLI code here, and the error message you
get, we could probably tell you more (though it would be somewhat
offtopic in a C# group, I guess).
One thing though - are you sure you did not forget "ref" in your C++/
CLI class declaration?
当我尝试从这个类继承
时我得到的错误br />
公共引用类MyDictionary:DictionaryBase< String ^,Object ^>
{
public:
MyDictionary(无效);
};
是
错误1错误C3766:''MyDictionary''必须提供
接口方法的实现''System :: Collections :: Generic :: IEnumerator< T>
^ System :: Collections :: Generic :: IEnumerable< T> :: Get Enumerator(void)''
错误2错误C3766:''MyDictionary''必须提供实现
接口方法''void
System :: Collections :: Generic :: ICollection< T> :: Add(System :: Collections :: Generic :: KeyValuePair< TKey ,TV alue>)''
错误3错误C3766:''MyDictionary''必须提供实现
接口方法''System :: Collections :: Generic :: ICollection< T>
^ System :: Collections :: Generic :: IDictionary< TKey, TV alue> :: Keys :: get(void)''
错误4错误C3766:''MyDictionary''必须提供
的实现接口方法''System :: Collections :: Generic :: ICollection< T>
^ System :: Collections :: Generic :: IDictionary< TKey,TV alue> :: Values :: get(void )''
" Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言
新闻:MP ******************** @ msnews.microsoft.com。 ......
The errors I am getting when I try to inherit from this class with
public ref class MyDictionary : DictionaryBase<String^, Object^>
{
public:
MyDictionary(void);
};
are
Error 1 error C3766: ''MyDictionary'' must provide an implementation for the
interface method ''System::Collections::Generic::IEnumerator<T>
^System::Collections::Generic::IEnumerable<T>::Get Enumerator(void)''
Error 2 error C3766: ''MyDictionary'' must provide an implementation for the
interface method ''void
System::Collections::Generic::ICollection<T>::Add( System::Collections::Generic::KeyValuePair<TKey,TV alue>)''
Error 3 error C3766: ''MyDictionary'' must provide an implementation for the
interface method ''System::Collections::Generic::ICollection<T>
^System::Collections::Generic::IDictionary<TKey,TV alue>::Keys::get(void)''
Error 4 error C3766: ''MyDictionary'' must provide an implementation for the
interface method ''System::Collections::Generic::ICollection<T>
^System::Collections::Generic::IDictionary<TKey,TV alue>::Values::get(void)''
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .
<" Howard Swope" < howard.swopeATnavteqDOTcom>写道:
<"Howard Swope" <howard.swopeATnavteqDOTcom>wrote:
>这个问题一直困扰着我。我创建了一个集合
类并在C#库中实现它。如果我继承了另一个C#程序集中的这个类并且它可以正常工作,但是如果我在这个类中继承了C ++
/ CLI程序集它就不会编译。
>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.
你能提供更多的细节而不是它不会编译吗?错误消息
将是一个良好的开端 - 一些示例(失败)代码会更好。
绝对理想是减少你的
集合类中的代码量,直到你遇到最小的失败。
Could you give more details than "it won''t compile"? The error message
would be a good start - some sample (failing) code would be better. The
absolute ideal would be to cut down on the amount of code in your
collection class as well, until you''ve got a minimal failure.
>这表示CLR中存在问题。
>This indicates a problem in the CLR.
为什么?我以为这会是C ++ / CLI编译器的问题
如果有的话。
-
Jon Skeet - < sk *** @ pobox.com>
网站: http://www.pobox.com/~skeet
博客: http://www.msmvps.com/jon.skeet
C#深度: http://csharpindepth.com
这篇关于这适用于C#,但不适用于C ++ / CLI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!