gpt4 book ai didi

c# - 定义一个接受不同参数的接口(interface)方法

转载 作者:可可西里 更新时间:2023-11-01 08:40:11 25 4
gpt4 key购买 nike

我的应用程序使用连接到 PC 的测量仪器。我想让使用来自不同供应商的类似仪器成为可能。

所以我定义了一个接口(interface):

interface IMeasurementInterface
{
void Initialize();
void Close();
}

到目前为止一切顺利。在测量之前,我需要设置仪器,这意味着不同仪器的参数会有很大不同。所以我想定义一个方法,该方法采用可以具有不同结构的参数:

interface IMeasurementInterface
{
void Initialize();
void Close();
void Setup(object Parameters);
}

然后我会将对象转换到我需要的任何地方。这是要走的路吗?

最佳答案

您最好提出一个抽象的“参数”类,该类由每个不同的仪器参数扩展...例如然后使用泛型确保将正确的参数传递给正确的类...

public interface IMeasurement<PARAMTYPE> where PARAMTYPE : Parameters
{
void Init();
void Close();
void Setup(PARAMTYPE p);
}

public abstract class Parameters
{

}

然后针对每个特定设备,

public class DeviceOne : IMeasurement<ParametersForDeviceOne>
{
public void Init() { }
public void Close() { }
public void Setup(ParametersForDeviceOne p) { }
}

public class ParametersForDeviceOne : Parameters
{

}

关于c# - 定义一个接受不同参数的接口(interface)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/238184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com