C ++ / CLI:实现IList和IList (显式实现默认索引器)

时间:2022-09-01 09:29:14

I am trying to implement a C++/CLI class that implements both IList and IList<T>.

我正在尝试实现一个实现IList和IList 的C ++ / CLI类。

Since they have overlapping names, I have to implement one of them explicitly, and the natural choice should be IList.

由于它们具有重叠的名称,我必须明确地实现其中一个,并且自然选择应该是IList。

The implicit implementation of the indexer is:

索引器的隐式实现是:

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);
  }
}

I am now trying to declare the default indexer for IList.

我现在正在尝试声明IList的默认索引器。

My guess would be something like this:

我的猜测是这样的:

  property Object^ System::Collections::IList::default[int]{
    virtual Object^ System::Collections::IList::get(int index);
    virtual void System::Collections::IList::set(int index, Object^ item);
  }

but that just gives me

但那只是给了我

error C2061: syntax error : identifier 'default'

错误C2061:语法错误:标识符'默认'

Any hints?

3 个解决方案

#1


JaredPar's answer almost worked. Two things should be changed:

JaredPar的答案几乎奏效了。应该改变两件事:

  • The indexer-property needs a different name since "default" is already taken by the implicit implementation.
  • indexer-property需要一个不同的名称,因为隐式实现已经采用“default”。

  • The specification of the overriding needs to be done on the set- and get-methods, not on the property itself.
  • 重写的规范需要在set-和get-methods上完成,而不是在属性本身上完成。

I.e.:

  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;
  }

#2


Haven't done a lot of interfaces in C++/CLI but this appears to be covered 8.8.10.1 of the C++/CLI spec. I believe the feature you're looking for is explicit overriding. In this you must specify the implemented member after the definition like so.

没有在C ++ / CLI中完成很多接口,但这似乎涵盖了C ++ / CLI规范的8.8.10.1。我相信你正在寻找的功能是明确的重写。在此,您必须在定义之后指定实现的成员,如此。

property Object^ default[int] = System::Collections::IList::default {... }

#3


I compiled a class implementing IList<T> explicitly written in C# and opened it with Reflector and disassembled to C++/CLI.

我编译了一个实现用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)
{
   //
}

But it doesn't compile: get_Item, set_Item is not a member of IList<T>;

但是它没有编译:get_Item,set_Item不是IList 的成员;

#1


JaredPar's answer almost worked. Two things should be changed:

JaredPar的答案几乎奏效了。应该改变两件事:

  • The indexer-property needs a different name since "default" is already taken by the implicit implementation.
  • indexer-property需要一个不同的名称,因为隐式实现已经采用“default”。

  • The specification of the overriding needs to be done on the set- and get-methods, not on the property itself.
  • 重写的规范需要在set-和get-methods上完成,而不是在属性本身上完成。

I.e.:

  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;
  }

#2


Haven't done a lot of interfaces in C++/CLI but this appears to be covered 8.8.10.1 of the C++/CLI spec. I believe the feature you're looking for is explicit overriding. In this you must specify the implemented member after the definition like so.

没有在C ++ / CLI中完成很多接口,但这似乎涵盖了C ++ / CLI规范的8.8.10.1。我相信你正在寻找的功能是明确的重写。在此,您必须在定义之后指定实现的成员,如此。

property Object^ default[int] = System::Collections::IList::default {... }

#3


I compiled a class implementing IList<T> explicitly written in C# and opened it with Reflector and disassembled to C++/CLI.

我编译了一个实现用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)
{
   //
}

But it doesn't compile: get_Item, set_Item is not a member of IList<T>;

但是它没有编译:get_Item,set_Item不是IList 的成员;