将指针函数移植到c#

时间:2014-08-11 17:50:59

标签: c#

我在C

中有以下内容
typedef void (*procfunc)(V2fT2f *, float);

typedef struct {
    procfunc func;
    procfunc degen;
} Filter;

const Filter filter[] = {
    { brightness             },
    { contrast               },
    { extrapolate, greyscale },
    { hue                    },
    { extrapolate, blur      }, // The blur could be exaggerated by downsampling to half size
};

我已经带到C#给我这个

public delegate void procfunc(ImagingDefs.V2fT2f[] quad,float t);

    public class Filter
    {
        public procfunc func;
        public procfunc degen;
    };

    public Filter[] filter = new Filter[]
    {
        new Filter { func = brightness },
        new Filter { func = contrast },
        new Filter { func = extrapolate, degen = greyscale },
        new Filter { func = hue },
        new Filter { func = extrapolate, degen = blur } // The blur could be exaggerated by downsampling to half size
    };

我的问题是我收到了错误

A field initializer cannot reference the nonstatic field, method or property.

我觉得问题出在代表身上,但我不确定 - 我以前从未需要移植过这类代码。

ImagingDefs和V2fT2f都没有声明为静态

被调用的方法通常是

public void foo(ImagingDefs.V2fT2f[]quad, float t)

没有任何东西是静止的。

名称V2fT2f来自原始源代码

1 个答案:

答案 0 :(得分:2)

字段初始值设定项不能引用非静态字段,方法或属性。指的是brightness等过滤方法。

如果要在字段初始值设定项filter中引用这些方法,则必须将这些方法设为静态。

另一种选择是在您定义的类的构造函数中初始化此字段。

E.g。

public class MyNewClass
{
    public delegate void procfunc(ImagingDefs.V2fT2f[] quad,float t);

    public class Filter
    {
        public procfunc func;
        public procfunc degen;
    };

    public Filter[] filter;

    public MyNewClass()
    {
        filter = new Filter[]
        {
            new Filter { func = brightness },
            new Filter { func = contrast },
            new Filter { func = extrapolate, degen = greyscale },
            new Filter { func = hue },
            new Filter { func = extrapolate, degen = blur } // The blur could be exaggerated by downsampling to half size
        };
    }   
}