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

时间:2016-07-02 11:34:15

标签: c# properties c++-cli

我在C ++ / CLI中有2个ref类:

>第一堂课:

 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;}
        }
    };

>第二节课:

    public ref class wBlobParams
    {
        wBlobFilter mFilter;

    public:

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

当我在C#中调用它时,我收到一条错误消息:"无法修改返回值,因为它不是变量"

        Params.Filter.Min = 0;

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

1 个答案:

答案 0 :(得分:0)

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

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);
}
相关问题