Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to find interfaces in code programmatically

How to find interfaces in code programmatically

Scheduled Pinned Locked Moved C#
helptutorialbusinessquestionannouncement
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Frygreen
    wrote on last edited by
    #1

    Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:

    void CreateReport( params here ) {
    CreateReport_1(par);
    CreateReport_2(par);
    .......
    }

    My idea is: Do validation, before starting the single reports

    Solution 1: Register all validation-delegates and execute them before starting the reports

    This kind of code is

    void CreateReport( some params ) {
    // GetValidationDelegate() are static functions
    myValidationList.Add( Reporter_1.GetValidationDelegate());
    myValidationList.Add( Reporter_2.GetValidationDelegate());

    foreach (validationDelegate in myValidationList) {
          execute validationDelegate;
    }
    

    // Validation is done, start now with reports
    CreateReport_1(par);
    CreateReport_2(par);
    .....
    }

    This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.

    Solution 2: Do it "more automatic", this is what I am looking for

    The kind of code I am looking for is

    void CreateReport( some params ) {
    myValidationList = GetAllAvailableValidationDelegates()
    foreach (validationDelegate in myValidationList) {
    execute validationDelegate;
    }
    // Validation is done, start now with reports
    CreateReport_1(par);
    .....
    }

    I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates

    L Kornfeld Eliyahu PeterK J B 5 Replies Last reply
    0
    • F Frygreen

      Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:

      void CreateReport( params here ) {
      CreateReport_1(par);
      CreateReport_2(par);
      .......
      }

      My idea is: Do validation, before starting the single reports

      Solution 1: Register all validation-delegates and execute them before starting the reports

      This kind of code is

      void CreateReport( some params ) {
      // GetValidationDelegate() are static functions
      myValidationList.Add( Reporter_1.GetValidationDelegate());
      myValidationList.Add( Reporter_2.GetValidationDelegate());

      foreach (validationDelegate in myValidationList) {
            execute validationDelegate;
      }
      

      // Validation is done, start now with reports
      CreateReport_1(par);
      CreateReport_2(par);
      .....
      }

      This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.

      Solution 2: Do it "more automatic", this is what I am looking for

      The kind of code I am looking for is

      void CreateReport( some params ) {
      myValidationList = GetAllAvailableValidationDelegates()
      foreach (validationDelegate in myValidationList) {
      execute validationDelegate;
      }
      // Validation is done, start now with reports
      CreateReport_1(par);
      .....
      }

      I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You would need to use reflection to find all the methods in a class: see https://www.google.com/search?q=reflection+enumerate+methods+c%23[^], for some samples.

      1 Reply Last reply
      0
      • F Frygreen

        Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:

        void CreateReport( params here ) {
        CreateReport_1(par);
        CreateReport_2(par);
        .......
        }

        My idea is: Do validation, before starting the single reports

        Solution 1: Register all validation-delegates and execute them before starting the reports

        This kind of code is

        void CreateReport( some params ) {
        // GetValidationDelegate() are static functions
        myValidationList.Add( Reporter_1.GetValidationDelegate());
        myValidationList.Add( Reporter_2.GetValidationDelegate());

        foreach (validationDelegate in myValidationList) {
              execute validationDelegate;
        }
        

        // Validation is done, start now with reports
        CreateReport_1(par);
        CreateReport_2(par);
        .....
        }

        This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.

        Solution 2: Do it "more automatic", this is what I am looking for

        The kind of code I am looking for is

        void CreateReport( some params ) {
        myValidationList = GetAllAvailableValidationDelegates()
        foreach (validationDelegate in myValidationList) {
        execute validationDelegate;
        }
        // Validation is done, start now with reports
        CreateReport_1(par);
        .....
        }

        I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates

        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu Peter
        wrote on last edited by
        #3

        I think learning MEF[^] can help you...

        I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

        "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

        1 Reply Last reply
        0
        • F Frygreen

          Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:

          void CreateReport( params here ) {
          CreateReport_1(par);
          CreateReport_2(par);
          .......
          }

          My idea is: Do validation, before starting the single reports

          Solution 1: Register all validation-delegates and execute them before starting the reports

          This kind of code is

          void CreateReport( some params ) {
          // GetValidationDelegate() are static functions
          myValidationList.Add( Reporter_1.GetValidationDelegate());
          myValidationList.Add( Reporter_2.GetValidationDelegate());

          foreach (validationDelegate in myValidationList) {
                execute validationDelegate;
          }
          

          // Validation is done, start now with reports
          CreateReport_1(par);
          CreateReport_2(par);
          .....
          }

          This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.

          Solution 2: Do it "more automatic", this is what I am looking for

          The kind of code I am looking for is

          void CreateReport( some params ) {
          myValidationList = GetAllAvailableValidationDelegates()
          foreach (validationDelegate in myValidationList) {
          execute validationDelegate;
          }
          // Validation is done, start now with reports
          CreateReport_1(par);
          .....
          }

          I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Frygreen wrote:

          I want to call both functions/interfaces without registering them before.

          What does "registering" mean here? Are you referring to the examples of Unity? You can ask a class which interfaces it implements; you'd still need to have "some place" where you define WHICH interface should be searched for.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          • F Frygreen

            Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:

            void CreateReport( params here ) {
            CreateReport_1(par);
            CreateReport_2(par);
            .......
            }

            My idea is: Do validation, before starting the single reports

            Solution 1: Register all validation-delegates and execute them before starting the reports

            This kind of code is

            void CreateReport( some params ) {
            // GetValidationDelegate() are static functions
            myValidationList.Add( Reporter_1.GetValidationDelegate());
            myValidationList.Add( Reporter_2.GetValidationDelegate());

            foreach (validationDelegate in myValidationList) {
                  execute validationDelegate;
            }
            

            // Validation is done, start now with reports
            CreateReport_1(par);
            CreateReport_2(par);
            .....
            }

            This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.

            Solution 2: Do it "more automatic", this is what I am looking for

            The kind of code I am looking for is

            void CreateReport( some params ) {
            myValidationList = GetAllAvailableValidationDelegates()
            foreach (validationDelegate in myValidationList) {
            execute validationDelegate;
            }
            // Validation is done, start now with reports
            CreateReport_1(par);
            .....
            }

            I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            Frygreen wrote:

            at startup of my program I want to find all instances/classes, which support an special interface.

            Your requirement as stated in general is difficult. However if your application is small and/or you limit the search parameters it becomes more reasonable. As an example of the difficulty involve one wouldn't want to, for example, search all of the Microsoft API classes for your interface since it just won't be there. So besides using reflection you should determine where you are going to search. You should also evaluate whether this is really worth while in terms of future plans. It might be easier to look into using Windows Addins (or whatever they are called) or in just hard coding a list or even self registration.

            1 Reply Last reply
            0
            • F Frygreen

              Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:

              void CreateReport( params here ) {
              CreateReport_1(par);
              CreateReport_2(par);
              .......
              }

              My idea is: Do validation, before starting the single reports

              Solution 1: Register all validation-delegates and execute them before starting the reports

              This kind of code is

              void CreateReport( some params ) {
              // GetValidationDelegate() are static functions
              myValidationList.Add( Reporter_1.GetValidationDelegate());
              myValidationList.Add( Reporter_2.GetValidationDelegate());

              foreach (validationDelegate in myValidationList) {
                    execute validationDelegate;
              }
              

              // Validation is done, start now with reports
              CreateReport_1(par);
              CreateReport_2(par);
              .....
              }

              This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.

              Solution 2: Do it "more automatic", this is what I am looking for

              The kind of code I am looking for is

              void CreateReport( some params ) {
              myValidationList = GetAllAvailableValidationDelegates()
              foreach (validationDelegate in myValidationList) {
              execute validationDelegate;
              }
              // Validation is done, start now with reports
              CreateReport_1(par);
              .....
              }

              I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              Not sure why this is downvoted? Perhaps it was very bad pre-edit. Anyway, what you are asking for is a classic plugin architecture, and that's a well solved problem. There are three steps to it:

              • Create a boundary interface that defines what the plugin should do. This interface should be in an assembly available to both the plugin developer (plugins need a compile time reference to the assembly) and the application. In the case of a releasable app or something where the same team develops both sides it can go in the main application assembly.
              • Create plugin classes which implement the interface and do the work.
              • Write some reflection code in your main application that finds instances of the plugins in relevant assemblies, and registers them.

              The exact implementation of that depends on whether you need AppDomain separation between plugins (for example if you want to support plugin update or unloading without restarting the application, or different security contexts), and whether plugins will be in separate assemblies. In this case the boundary interface is

              public interface IReportGenerator {
              void Validate();
              void Execute();
              }

              ... although I'd expect some form of input to be passed to both methods in reality. The simplest version of the reflection code (look in a single assembly only) looks like this:

              public void RegisterPlugins(Assembly a) {
              foreach(Module mod in a.GetLoadedModules()){
              foreach(Type ty in mod.GetTypes()){
              foreach(Type intf in ty.GetInterfaces()){
              if(intf == typeof(IReportGenerator)){
              RegisterPlugin((IReportGenerator)Activator.CreateInstance(ty));
              }
              }
              }
              }
              }

              For a full implementation including looking up assemblies in a directory and loading each plugin into an AppDomain, check out my game lobby article[^] (LobbyClient/GameType.cs or LobbyServer/GameTypes.cs). Although don't necessarily copy the coding style in there, that is old code from before I knew what I was doing.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups