Search code examples
c#c++-cliogre

IEnumerable or List Property in a C# interface


I'm trying to design an interface in C# that's being used in C++ CLI afterwards.

The C++ CLI class that implements the C# interface needs to have a list like property that can be iterated through. This class is actually a wrapper for a native (if you want the full details, it contains a pointer to an Ogre RenderWindow object that has a list of Viewports, each Viewport item is easily accessible through a RenderWindow property called getViewport(int index) ).

The goal is to have a property in the interface AND in the C++ CLI implementation of this interface, such that it can be used with the for each construct. What would be the cleanest way to do this?

Some snippets of the discussed items:

The C# interface:

public interface IRenderWindow
    {
         bool IsActive { get; set; }
    }

And the C++ CLI implementer

public ref class CLIOgreRenderWindow : public IRenderWindow
        {
        private:
            Ogre::RenderWindow * mRenderWindow;
        public:
            CLIOgreRenderWindow();

            virtual property bool IsActive 
            {
                bool get() sealed {return mRenderWindow->isActive();}
                void set(bool value) sealed { mRenderWindow->setActive(value); }
            }
        }

Solution

  • If I understand your question correctly, you want to expose the list of Viewports. If that is the case, then you can:

    1. Create a wrapper ref class for your native Viewport objects (just like you did for CLIOgreRenderWindow.
    2. Have the IRenderWindow expose a IEnumerable with a getter.
    3. In your C++/CLI implementation you could create a managed Array/List of the wrapper objects (based on the native objects), and return that.

    Item #3 above is where you can do something different depending on your requirements (i.e. you could implement IEnumerable to yield one result at a time--though that's a bit more work).