Search code examples
c#collectionssystem.reflection

C# Load Collection of Classes in a List


I have a program that I have created a small database. Each device(class) in the database uses a Interface. I am loading each driver at runtime from a text file that reads the device configuration and loads the appropriate driver with reflection. I can see in console each device get created and can invoke a method at creation. The problem is I want to create the drivers, add them to a collection of devices, then call them by enumerating the list. So far every time it only call the last device added to the list.

Here is the code that adds the devices to the list:

if (loadedDisplay != null)
{
    try
    {
        CrestronConsole.PrintLine("Display Class is valid + {0}", loadedDisplay.ToString());
        if (videoDisplay.isCrestronConnected)
        {
            CrestronConsole.PrintLine("Display is Crestron Connected");
            loadedDisplay.RegisterNewDisplay(videoDisplay.CrestronConnectedIPID, videoDisplay.Name, Global.ControlSystem);
            loadedDisplay.DisplayPowerEvent += new EventHandler<DisplayPowerEventArgs>(loadedDisplay_DisplayPowerEvent);
            loadedDisplay.DisplayMuteEvent += new EventHandler<DisplayMuteEventArgs>(loadedDisplay_DisplayMuteEvent);
            loadedDisplay.DisplayOnlineChangeEvent += new EventHandler<DisplayOnlineEventArgs>(loadedDisplay_DisplayOnlineChangeEvent);
        }
        else if (!videoDisplay.isCrestronConnected)
        {
            CrestronConsole.PrintLine("Display is not Crestron Connected");
            foreach (var port in Global._rmcComports)
            {
                CrestronConsole.PrintLine(videoDisplay.Name + " On Port  " + port.port);
                if (videoDisplay.Name == port.portDescrition)
                {
                    try
                    {
                        loadedDisplay.SetComSpec(port.port, videoDisplay.Name);
                    }
                    catch (Exception e)
                    {
                        CrestronConsole.PrintLine("Exception thrown : " + e.Message);
                    }
                }
            }
        }
        try
        {
            DisplayCollection.systemDisplayList.Add(new SystemDisplays
            {
                displayInterface = loadedDisplay,
                displayDescrition = videoDisplay.Name,
                PowerOnJoin = videoDisplay.PowerOnJoin,
                PowerOffJoin = videoDisplay.PowerOffJoin,
                MuteToggleJoin = videoDisplay.MuteToggleJoin
            });
        }
        catch (Exception e)
        {
            CrestronConsole.PrintLine("Error adding to list : " + e.Message);
        }
    }
    catch (Exception error)
    {
        CrestronConsole.PrintLine(LogHeader + "Failed to create instance : {0}", error.Message);
    }                   
}

at this point i am able to call methods such as the RegisterNewDsiplay or SetComSpec.

then later when a button on my UI is pressed I enumerate the collection and call the appropriate method.

Example:

switch (joinNumber)
{
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    {
        if (args.Sig.BoolValue == true)
        {
            foreach (var display in DisplayCollection.systemDisplayList)
            {
                if (display.PowerOnJoin == joinNumber)
                {
                    display.displayInterface.DisplayPowerOn();
                }
                else if (display.PowerOffJoin == joinNumber)
                {
                    display.displayInterface.DisplayPowerOff();
                }
            }
        }
        break;
    }
}

At this point if i call the PowerOn or PowerOff it only sends the request to the last display that was added tot he collection even if the PowerOnJoin != joinNumber.

Any ideas?


Solution

  • So after much research I realized my problem. The list i was creating was not the correct collection of data.

    I had to create a List of the Interface:

    static public List<Framework.DisplayDrivers.IDisplayInterface> systemDisplayList = new List<Framework.DisplayDrivers.IDisplayInterface>();
    

    after I could then add new instances of the classes to the Interface List and call methods and set any variables needed:

      DisplayCollection.systemDisplayList.Add(new Framework.DisplayDrivers.CrestronConnectedDisplay
                               {
                                   DisplayName = videoDisplay.Name,
                                   PowerOnJoin = videoDisplay.PowerOnJoin,
                                   PowerOffJoin = videoDisplay.PowerOffJoin,
                                   isCrestronConnected = videoDisplay.isCrestronConnected,
                                   displayIPID = videoDisplay.CrestronConnectedIPID,
                                   CS = Global.ControlSystem
                               });