Explicit override using automatic property

I am trying to use an Auto-implemented C ++ / CLI property to explicitly override an interface. Specifically, I wrote (in C ++ / CLI)

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

      

To explicitly consume IInterface

in C # one has to 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, you 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;
}

      

gives compiler errors C2146

: missing ;

before the identifier Object

, C2433

: virtual

not permitted under these ads C4430

: missing type specifier and C3766

: an interface member is not implemented. Am I missing something? What is the appropriate C ++ / CLI syntax to achieve what I am looking for?

+3


source to share


1 answer


I don't think you can use a base property (ie: auto-implemented) with a different name to explicitly implement an interface property. However, your explicitly implemented property can reference the underlying 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();
        }
    }
};

      

+1


source







All Articles