C# C动态创建类实例

C# C动态创建类实例,c#,class,dynamic,automation,C#,Class,Dynamic,Automation,我现在的处境是,我不知道我不知道什么,所以我不确定这是否是解决这个问题的正确方法,如果这被认为是无知的话,我道歉 我有一个连接到以太网控制器的程序。该程序允许用户配置连接到系统的内容并设置I/O通信 每个控制器都是自己的设备,并且可能具有不同的IO,具体取决于它的型号。控制器有自己的API 程序将配置保存到XML配置文件中,该文件在启动时读取。然后,我需要连接到每个未知设备,并设置到每个设备的连接,这样我就可以在以后引用每个设备 以下是我努力实现的目标: using Brainboxes.IO

我现在的处境是,我不知道我不知道什么,所以我不确定这是否是解决这个问题的正确方法,如果这被认为是无知的话,我道歉

我有一个连接到以太网控制器的程序。该程序允许用户配置连接到系统的内容并设置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();
    }
}

听起来你好像想同时做很多事情。解释一下您想要的内容:查看您的问题、示例代码和注释

当应用程序启动时,您希望它自动连接到不同设备的集合 运行时,用户可以连接并配置正确的设备 确保应用程序停止时连接已关闭 你的问题也是开放式的,从你的第一句话开始,我假设你是一个初学者。我知道这很枯燥,但是您必须查找您正在使用的硬件的文档。幸运的是,它看起来很漂亮

你需要给你的类一个更具代表性的名字。例如BrainboxController或BrainboxManager,从声音上看,这就是它的用途。 看起来BB400是一种可能的硬件设备,它是继承层次结构的一部分,所以您不想仅限于此 我会避免在构造函数中做很多工作,这会使发现问题更加困难 使用字典存储您的设备,这就是您以后引用每个设备的方式
最后,不管您试图用Outputs方法做什么,它看起来像是业务逻辑,它也应该存在于其他地方。

非常感谢您的帮助。这对我真的很有帮助。我看过前面提到的词典,但并不真正理解它们的用法。我的背景是机械工程师,所以我主要自学代码。您的详细回答非常有用,再次感谢您。您可以通过实现IEnumerable接口的任何类型进行循环,并在ID等字段上进行匹配。字典数据结构经过优化,可用于快速查找,但它还提供了一种更具可读性的方式来获取代码中的值。
public class BrainboxController : IDisposable
{
    private readonly HashSet<string> _deviceIps; // potentially you can get away without having this if you call InitialiseDevices() in the constructor

    private Dictionary<string, EDDevice> _devices = new Dictionary<string, EDDevice>(); // possibly use IDevice<C, P> instead of EDDevice

    public BrainboxController(IEnumerable<string> devices)
    {
        _deviceIps = new HashSet<string>(devices);
    }

    public void InitialiseDevices()
    {
        foreach (string ip in _deviceIps)
            _devices.Add(ip, EDDevice.Create(ip));
    }

    public void AddDevice(string ip)
    {
        if (_deviceIps.Add(ip))
            _devices.Add(ip, EDDevice.Create(ip));
    }

    public void RemoveDevice(string ip)
    {
        if(_devices.ContainsKey(ip))
        {
            var device = _devices[ip];
            device.Disconnect();
            device.Dispose();
            _devices.Remove(ip);
            _deviceIps.Remove(ip);
        }
    }

    public EDDevice GetDevice(string deviceIp)
    {
        if (_devices.ContainsKey(deviceIp))
            return _devices[deviceIp];

        return null;
    }

    public string GetConfiguration(string deviceIp)
    {
        if (_devices.ContainsKey(deviceIp))
            return _devices[deviceIp].Describe(); // I'm assuming that this gets the config data

        return "Device not found";
    }

    public bool SetConfiguration(string deviceIp, string xml)
    {
        if (_devices.ContainsKey(deviceIp))
        {
            _devices[deviceIp].SendCommand(xml); // I'm assuming this is how the config data is set
            return true;
        }

        // log device not found
        return false;
    }

    public IOList<IOLine> GetOutputs(string deviceIp, int relay)
    {
        if (_devices.ContainsKey(deviceIp))
            return _devices[deviceIp].Outputs[relay];

        // log device not found
        return new IOList<IOLine>();

    }

    public void Dispose()
    {
        foreach(var device in _devices.Values)
        {
            device.Disconnect();
            device.Dispose();
        }
    }
}
var deviceAddresses = new[] { "192.168.16.147", "192.168.16.148", "192.168.16.149", "192.168.16.150" };
var controller = new BrainboxController(deviceAddresses);
controller.InitialiseDevices();
var currentDevice = controller.GetDevice("192.168.16.147");
// do something with currentDevice