tags:

views:

326

answers:

2

I'm trying to convert this c# code into c++/cli code:

class MyRange : IEnumerable<int>
{
    public IEnumerator<int> GetEnumerator() { return null; }

    IEnumerator IEnumerable.GetEnumerator() { return null; }
}

Here is my attempt:

namespace Tests {
    public ref class MyRange : System::Collections::Generic::IEnumerable<int> {
    private:
     virtual System::Collections::IEnumerator^ GetEnumerator() = System::Collections::IEnumerable::GetEnumerator {
      return nullptr;
     }

     virtual System::Collections::Generic::IEnumerable<int>^ GetEnumerator() {
      return nullptr;
     }
    };
}

It's giving me so many errors (like 20), that I don't even think it's worth putting them here.

I've googled it all and it seems like a lot of people are having the same problem as me.

+2  A: 

It's often helpful to disassemble your C# in Reflector using Managed C++ as the target language and then from there do the translation to C++/CLI.

Tom Kirby-Green
Reflector's code don't work :(
devoured elysium
+3  A: 

Ok, after a lot of fighting found some working code:

namespace Tests {
    ref class MyCollection : public Generic::IEnumerable<int>
    {
    public:
     virtual System::Collections::IEnumerator^ GetEnumeratorNonGeneric() = System::Collections::IEnumerable::GetEnumerator
     {
      return GetEnumerator();
     }

     virtual Generic::IEnumerator<int>^ MyCollection::GetEnumerator()
     {
      return nullptr;
     }
    };
}
devoured elysium