How to find the class that implemented an interface?


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.

I have different classes implementing the interfaces ITest1 or ITest2 or both.

class1 : ITest1
class2 : ITest1, ITest2
class3 : ITest2

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

ITest1 test1;
ITest2 test2;
switch (configresult)
    case "configuration1":
        test1 = new class1();
        test2 = new class2();
    case "configuration2":
        test1 = new class2();
        test2 = new class3();
    case "configuration3":  // This happens the most!
        test1 = new class2();
        test2 = (class2)test1;

Now I would like to know during runtime what class implements the certain interface. In pseudocode:

if( test1 is implemented by class2 )

if( test2 is implemented by class1 )

Is there a way to find out the class used to implement an interface by having the interface variable available?

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.


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.

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

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.

There's the is keyword documented here although it sounds like from your question you've got bigger problems in your architecture.

if( test1 is class2 )

if( test2 is class1 )