Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I find the class that implemented an interface
    text
    copied!<p>I have an architectural question for my WinForms, C# based application. I really hope somebody can give me an idea of how to solve this.</p> <p>I have different classes implementing the interfaces ITest1 or ITest2 or both.</p> <pre><code>class1 : ITest1 class2 : ITest1, ITest2 class3 : ITest2 </code></pre> <p>At the start of the application I have some configuration source that tells the application what classes to use during the entire lifetype of the application(class1, class2 or class3) for ITest1 and ITest2</p> <pre><code>ITest1 test1; ITest2 test2; switch (configresult) { case "configuration1": test1 = new class1(); test2 = new class2(); break; case "configuration2": test1 = new class2(); test2 = new class3(); break; case "configuration3": // This happens the most! test1 = new class2(); test2 = (class2)test1; break; } </code></pre> <p>Now I would like to know during runtime what class implements the certain interface. In pseudocode:</p> <pre><code>if( test1 is implemented by class2 ) ((class2)test1).Class2Method(); if( test2 is implemented by class1 ) ((class1)test1).Class1Method(); </code></pre> <p>Is there a way to find out the class used to implement an interface by having the interface variable available?</p> <p>In the real program I have over 10 interfaces that are implemented by 40+ classes in completely different ways, so I would not like to do any switch statements for all moments I need to cast back to the original class.</p> <p>------------------UPDATE!</p> <p>I guess some background info makes it easier to get my problem and yes I agree with Ed S. that it is more a question about my design, but I am very much willing to change it all.</p> <p>My scientific application supports over 40+ different scientific instruments to measure semiconductors. Temperaure, current, voltage, switches, you name it. I defined 10 functionalities that are available to the user: temperature, voltage, current,switching, etc.... These are all put in interfaces; ITemperature, IVoltage, etc...</p> <p>Some instruments can do everything and some can only do parts. If the user has a supported high quality temperature measurement device, we use that to implement ITemperature, however when he/she has an expensive voltmeter that also can measure a temperture I use the same instrument to measure Temperature and Voltage (so the instrument implements ITemperature and IVoltage). All instruments are very, very different, it is hardly possible to make one abstract class to cover them all. Some need to be controlled over a webinterface, some over usb and believe me many things are different. So sometimes I need to access the instrument class directly (to reset a register, initialize or whatever). This is what I try to do. Any suggestion is very very welcome.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload