C++/CLI:如何编写 属性 的 属性

C++/CLI: How to write property of the property

我在 C++/CLI 中有 2 个引用 class:

>第一个class:

 public ref class wBlobFilter
    {
        int  mMin;
        int  mMax;
        bool mIsActive;

    public:

        //  min value
        property int Min
        {
            int get() {return mMin;}
            void set(int value) {mMin = value;}
        }

        //  max value
        property int Max
        {
            int get(){return mMax;}
            void set(int value){mMax = value;}
        }

        //  set to true to active
        property bool IsActive
        {
            bool get() {return mIsActive;}
            void set(bool value){mIsActive = value;}
        }
    };

>第二个class:

    public ref class wBlobParams
    {
        wBlobFilter mFilter;

    public:

        property wBlobFilter Filter
        {
            wBlobFilter get() {return mFilter;}
            void set(wBlobFilter value) { mFilter = value; }
        }
    };

当我在 C# 中调用它时,我收到一条错误消息:"Cannot modify the return value because it is not a variable"

        Params.Filter.Min = 0;

那么,如何直接通过classwBlobParams的属性设置classwBlobFilter的成员变量的值呢?对不起,我的英语不好。谢谢!!!

很难知道你到底想要发生什么。如果它继承,则过滤器的属性将可用。

public ref class wBlobParams : public wBlobFilter
{};

void f(wBlobParams^ params) {
    auto max = params->Max;
}

或者在 wBlobParams 中复制 属性 访问:

public ref class wBlobParams {
public:
    wBlobFilter^ mFilter;

    property int Max {
        int get() { return mFilter->Max; }
    }
};

void f(wBlobParams^ params) {
    auto max = params->Max;
}

编辑 1:
看这个。你做的很好。只是您使用 gc 句柄的语法是错误的。

public ref class cA {
    int x;

public:
    cA() : x(0) {}

    property int X  {
        int get() { return x; }
        void set(int _x) { x = _x; }
    }
};

public ref class cB {
    cA^ a;
public:
    cB() : a(gcnew cA()) {}

    property cA^ A  {
        cA^ get() { return a; }
        void set(cA^ _a) { a = _a; }
    }
};

void main() {
    cB^ b = gcnew cB();
    b->A->X = 5;
    Console::WriteLine(b->A->X);
}