C# 从父类重写DllImport方法

C# 从父类重写DllImport方法,c#,dll,static,parent-child,C#,Dll,Static,Parent Child,我有一个父类类,它利用.dll从中导入函数: class Parent { [DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int dllFunction(); } 现在我想创建一个子类来测试父类的功能,而不使用.dll中的方法。我不想使用.dll方法,因为.dll方法与外部传感器通信,我想测试代码而不需要此传感器的输入。因此,我需要重新定义

我有一个
父类
类,它利用
.dll
从中导入函数:

class Parent
{
    [DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int dllFunction();
}
现在我想创建一个
子类
来测试
父类
的功能,而不使用
.dll
中的方法。我不想使用
.dll
方法,因为
.dll
方法与外部传感器通信,我想测试代码而不需要此传感器的输入。因此,我需要重新定义
.dll
方法,以便模拟传感器的行为:

class Child : Parent
{
    public override int dllFunction()
    {

    }
}

当前的
Child.dllFunction()
方法不起作用,因为
Parent.dllFunction()
静态的
?是否可以覆盖
子类中
父类的
静态方法?或者您还有其他建议吗?

我建议您这样做:

使父类函数私有。创建一个调用它的公共函数,这样它就不会被直接调用

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int dllFunction();

public virtual int dllFunctionCaller()
{
    return dllFunction();
}

在您的子类中,改为覆盖dllFunctionCaller。

我建议改为这样做:

使父类函数私有。创建一个调用它的公共函数,这样它就不会被直接调用

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int dllFunction();

public virtual int dllFunctionCaller()
{
    return dllFunction();
}
在子类中,改为覆盖dllFunctionCaller