【发布时间】:2020-05-11 00:26:30
【问题描述】:
我处于“我不知道我不知道什么”的情况,所以我不确定这是否是解决这个问题的正确方法,如果出现这种情况,我们深表歉意一样的无知。
我有一个连接到以太网控制器的程序。该程序允许用户配置连接到系统的内容并设置 I/O 通信。
每个控制器都是自己的设备,并且可能有不同的 IO,具体取决于它的型号。控制器有自己的 API。
程序将配置保存到启动时读取的 XML 配置文件。然后我需要连接到每个未知设备并建立到每个设备的连接,让我有一种方法可以在以后引用每个设备。
这是我想要实现的目标:
using Brainboxes.IO;
public class BrainBoxes
{
public string[] Devices = new string[] { "192.168.16.147", "192.168.16.148", "192.168.16.149", "192.168.16.150" };
List<string> EDDeviceList = new List<string>();
public BrainBoxes() // set up devices and connections to all devices connected in the constructor
{
foreach (string Device in Devices)
{
EDDevice BB400 = EDDevice.Create("192.168.16.147");
// BB400 is a typical name but how do I make this dynamic at the same time making it
// available for other members of the class?
EDDeviceList.Add(BB400); // add the device to a list to refer to later in the constructor
}
for (int i = 0; i < EDDeviceList.Count - 1; i++) { BB400.Connect()}; // connect to each device in sequence.
}
public void Outputs(int Relay)
{
// this would be a switch statement
BB400.Outputs[Relay].Value = 1;
Thread.Sleep(75);
BB400.Outputs[Relay].Value = 0;
}
~BrainBoxes()
{
BB400.Disconnect();
}
}
【问题讨论】:
标签: c# class dynamic automation