C# Problem Loading Classes via Reflection into a collection

Chris M 1 Reputation point
2022-03-02T00:52:59.307+00:00

Hello all.
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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,354 questions
{count} votes