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:

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:

public interface class IItemBuilder
{
    property Item^ Prototype { Item^ 
get()};
};

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

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

No Comments