.net 实现IEnumerable<;T>;在C++/CLI

.net 实现IEnumerable<;T>;在C++/CLI,.net,c++-cli,.net,C++ Cli,我在C++/CLI中的自定义集合类中实现IEnumerable时遇到问题。以下是守则的相关部分: using namespace System::Collections::Generic; ref class MyCollection : IEnumerable<MyClass^> { public: MyCollection() { } virtual IEnumerator<MyClass^>^ GetEnumerator()

我在C++/CLI中的自定义集合类中实现
IEnumerable
时遇到问题。以下是守则的相关部分:

using namespace System::Collections::Generic;

ref class MyCollection : IEnumerable<MyClass^>
{
public:
    MyCollection()
    {
    }  

    virtual IEnumerator<MyClass^>^ GetEnumerator()
    {
        return nullptr;
    }
};
如果我添加了
IEnumerable::GetEnumerator()
的实现,编译器会抱怨两个方法只在返回类型上有所不同(这也是有意义的)


那么,如何在C++/CLI类中实现
IEnumerable

您必须提供非泛型命名空间的显式实现,并包括该命名空间:

using namespace System::Collections;

....

virtual IEnumerator^ EnumerableGetEnumerator() = IEnumerable::GetEnumerator
{
    return GetEnumerator<MyClass^>();
}
使用名称空间系统::集合;
....
虚拟IEnumerator ^EnumerableGetEnumerator()=IEnumerable::GetEnumerator
{
返回GetEnumerator();
}
更新:如注释中所述,GetEnumerator的显式版本必须命名为不同的,以避免名称冲突,因此我将其命名为EnumerableGetEnumerator

类似地,在C#中,您必须这样做:

class MyCollection : IEnumerable<MyClass>
{
    public MyCollection()
    {
    }

    public IEnumerator<MyClass> GetEnumerator()
    {
        return null;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
using System.Collections.Generic;

public class MyCollection : IEnumerable<MyClass>
{
    public MyCollection()
    {
    }  

    public IEnumerator<MyClass> GetEnumerator()
    {
        return null;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator<MyClass>();
    }
}
使用System.Collections.Generic;
公共类MyCollection:IEnumerable
{
公共MyCollection()
{
}  
公共IEnumerator GetEnumerator()
{
返回null;
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}

这不是很简单。这是我的尝试。填空。最大的问题之一是,如果同时使用Collections和Collections::Generic名称空间,则会产生歧义。C++/CLI真是一个难题

using namespace System;
using namespace System::Collections;

public ref struct Enumerable : public Generic::IEnumerable<String^> {

public:
    virtual Generic::IEnumerator<String^>^ GetEnumerator() sealed = Generic::IEnumerable<String^>::GetEnumerator { 
        return gcnew Enumerator(); 
    }

    virtual IEnumerator^ GetEnumeratorBase() sealed = IEnumerable::GetEnumerator { 
        return GetEnumerator(); 
    }

private:
    ref struct TagEnumerator : public Generic::IEnumerator<String^> {

    public:
        property String^ Current { 
            virtual String^ get() {
                throw gcnew NotImplementedException(); 
            } 
        };

        property Object^ CurrentBase { 
            virtual Object^ get() sealed = IEnumerator::Current::get { 
                throw gcnew NotImplementedException(); 
            } 
        };

        virtual bool MoveNext() { 
            throw gcnew NotImplementedException(); 
        }

        virtual void Reset() { 
            throw gcnew NotImplementedException(); 
        }

        virtual ~Enumerator() {
        }
    };
};
使用名称空间系统;
使用名称空间系统::集合;
公共引用结构可枚举:公共泛型::IEnumerable{
公众:
虚拟泛型::IEnumerator ^GetEnumerator()sealed=泛型::IEnumerable::GetEnumerator{
返回新的枚举数();
}
虚拟IEnumerator ^GetEnumeratorBase()sealed=IEnumerable::GetEnumerator{
返回GetEnumerator();
}
私人:
ref-struct标记枚举器:公共泛型::IEnumerator{
公众:
属性字符串^Current{
虚拟字符串^get(){
抛出gcnew NotImplementedException();
} 
};
属性对象^CurrentBase{
虚拟对象^get()sealed=IEnumerator::Current::get{
抛出gcnew NotImplementedException();
} 
};
虚拟bool MoveNext(){
抛出gcnew NotImplementedException();
}
虚拟空重置(){
抛出gcnew NotImplementedException();
}
虚拟~枚举器(){
}
};
};

仅仅实现GetEnumerator()是行不通的。如果我这样做了,我会得到一个编译器错误,指出重载函数不能只因返回类型不同而不同。更新了我的问题,我将如何在C中实现它?我想我在这里有点快。。。现在应该可以了。如果不行,我就开枪打自己的脚。是的,这很有效。我刚从谷歌上找到了同样的东西。谢谢你的帮助。:)一个细微的更改:第一个代码示例中的GetEnumerator()需要重命名为其他名称,以避免名称冲突。我使用了GetEnumerator2(),虽然这并不重要。谢谢。“如果同时使用Collections和Collections::Generic namespace,最大的问题之一是不明确”在
CurrentBase
的getter中不应该有
return Current