C++ cli C++/CLI:实施IList和IList<;T>;(默认索引器的显式实现)
我正在尝试实现一个C++/CLI类,它同时实现了C++ cli C++/CLI:实施IList和IList<;T>;(默认索引器的显式实现),c++-cli,indexer,explicit-interface,C++ Cli,Indexer,Explicit Interface,我正在尝试实现一个C++/CLI类,它同时实现了IList和IList 由于它们有重叠的名称,我必须显式地实现其中一个,自然选择应该是IList 索引器的隐式实现是: using namespace System::Collections::Generic; generic<class InnerT> public ref class MyList : public System::Collections::IList, IList<InnerT> { // ...
IList
和IList
由于它们有重叠的名称,我必须显式地实现其中一个,自然选择应该是IList
索引器的隐式实现是:
using namespace System::Collections::Generic;
generic<class InnerT> public ref class MyList : public System::Collections::IList, IList<InnerT> {
// ...
property InnerT default[int]{
virtual InnerT get(int index);
virtual void set(int index, InnerT item);
}
}
但那只会让我
错误C2061:语法错误:标识符“默认”
有什么提示吗?我编译了一个类,实现了用C#显式编写的
IList
,并用Reflector打开它,然后反汇编成C++/CLI
T System::Collections::Generic::IList<T>::get_Item(Int32 __gc* index)
{
//
}
void __gc* System::Collections::Generic::IList<T>::set_Item(Int32 __gc* index, T value)
{
//
}
T系统::集合::通用::IList::获取项目(Int32\uu gc*索引)
{
//
}
void\uuuu gc*系统::集合::通用::IList::设置\u项(Int32\uuuu gc*索引,T值)
{
//
}
但是它没有编译:
get_Item
,set_Item
不是IList
的成员 还没有在C++/CLI中完成很多接口,但本手册的8.8.10.1中似乎介绍了这一点。我相信您正在寻找的功能是显式覆盖。在这种情况下,必须像这样在定义之后指定实现的成员
property Object^ default[int] = System::Collections::IList::default {... }
几乎成功了。应该改变两件事:
- indexer属性需要另一个名称,因为隐式实现已经使用了“default”
- 重写的指定需要在set-and-get方法上完成,而不是在属性本身上完成
请将“同时实现IList和IList”更改为“同时实现IList和IList”。谢谢!似乎MaLDON正在移除.反射器,将其分解为托管C++而不是C++ +CLI。托管C++是旧的托管代码扩展。您可以通过所有的gc指针来判断。Reflector有一个扩展,可以生成C++/CLI。不幸的是,它还提供了不可编译的代码。在实现枚举器当前属性时,这也很重要:
property Object^ default[int] = System::Collections::IList::default {... }
property Object^ IListItems[int]{
virtual Object^ get(int index) = System::Collections::IList::default::get;
virtual void set(int index, Object^ item) = System::Collections::IList::default::set;
}