您可以使用字典或类似结构来跟踪您创建的所有项目。
public Dictionary<string, Controller> AllPLCs { get; set; } public Dictionary<string, TagGroup> ScanGroups { get; set; } public Dictionary<string, WithEvents> AllNewTags { get; set; }
从这里,您可以从数据库加载并使用名称作为字符串键添加到循环中的字典。
foreach(var foundItem in LoadedData) { AllPLCs.Add(foundItem.Name, new Controller()); ScanGroups.Add(foundItem.Name, new TagGroup()); AllNewTags.Add(foundItem.Name, new WithEvents()); }
稍后您可以按如下方式访问每个:
foreach(var foundItem in LoadedData) { Controller currentController = AllPLCs[foundItem.Name] as Controller()); TagGroup currentGroup = ScanGroups[foundItem.Name] as TagGroup; WithEvents currentEvent = AllNewTags[foundItem.Name] as WithEvents; }
我的代码示例是在C#中,但它们应该让您走上正确的轨道。