2

I'm trying to use a C++/CLI auto-implemented property to explicitly override an interface. In particular, I have written (in C++/CLI)

interface IInterface
{
    property Object ^MyProperty
    {
        Object ^get(void);
        void set(Object^);
    }
    void Method(void);
}

To explicitly consume IInterface in C#, one would write

class MyClass : IInterface
{
    Object IInterface.MyProperty { get; set;}
    void IInterface.Method()
    {
    }
}

C++/CLI does not support EII, but it does support explicit overrides. For example, one can write

sealed ref class MyClass : IInterface
{
private:
    virtual void method(void) = IInterface::Method {}
public:
    property Object ^MyProperty;
}

I want to define my explicit override using an auto-implemented property, but

sealed ref class MyClass : IInterface
{
private:
    virtual void method(void) = IInterface::Method {}
    property Object ^myProperty = IInterface::MyProperty;
}

yields the compiler errors C2146: Missing ; before identifier Object, C2433: virtual not permitted on data declarations, C4430: Missing type specifier, and C3766: Interface member not implemented. Am I missing something? What is the appropriate C++/CLI syntax to achieve what I seek?

Jacob Manaker
  • 723
  • 4
  • 17

1 Answers1

1

I don't think you can use a basic (i.e: auto-implemented) property with a different name to implement an interface property explicitly. However, your explicitly implemented property can refer to a basic property.

interface class IInterface
{
    property Object^ Property
    {
        Object^ get();
        void set(Object^ value);
    }
};

ref class MyClass sealed : IInterface
{
public:
    property String^ MyProperty;
private:
    virtual property Object^ UntypedProperty
    {
        Object^ get() sealed = IInterface::Property::get {
            return MyProperty;
        }

        void set(Object^ value) sealed = IInterface::Property::set {
            MyProperty = value->ToString();
        }
    }
};
Sebacote
  • 690
  • 6
  • 17