将指针函数移植到c#

本文关键字:指针 函数 | 更新日期: 2023-09-27 18:17:25

我在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.

我有一种感觉,问题出在委托上,但我不确定——我以前从未需要移植这种代码。

imagingdef和V2fT2f都没有被声明为静态

被调用的方法通常是

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

没有任何地方是静态的

名称V2fT2f来自原始源代码

将指针函数移植到c#

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

如果要在字段初始化器filter中引用这些方法,则必须将它们设置为静态

另一个选择是在你所定义的类的构造函数中初始化这个字段。

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