Christoph Rüegg

Math.NET, distributed computing and how an electrical engineer sees the world of complex software

C++/CLI: Explicitly Implement Interface Properties

In case you ever wondered how to explicitly implement interface properties in C++/CLI that collide with an already existing better typed property: It’s quite easy, but I didn’t find a sample in neither the language specification nor the web.

Imagine you have some already existing generic ItemBuilder class:

1
2
3
4
5
6
7
8
9
ref class Item;

generic<class T>
where T : Item
public ref class ItemBuilder
{
public:
    property T Prototype { T get(); };
};

But now you want to generalize it, to be able to access the prototype property as Item, independent of the actual generic class type. So you define an interface IItemBuilder:

1
2
3
4
public interface class IItemBuilder
{
    property Item^ Prototype { Item^ get(); };
};

How to explicitly implement this interface in ItemBuilder in C++/CLI?

1
2
3
4
5
6
7
8
9
10
generic<class T>
where T : Item
public ref class ItemBuilder : public IPacketBuilder
{
private:
    virtual property Item^ PrototypeUntyped
        { Item^ get() sealed = IItemBuilder::Prototype::get; };
public:
    property T Prototype { T get(); };
}; 

Let me know if you know a better approach…

Comments