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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Invoke Constructor method of any class that extends an Interface without having to specifically cast each class

Invoke Constructor method of any class that extends an Interface without having to specifically cast each class

Scheduled Pinned Locked Moved C#
questiondesignhelp
4 Posts 4 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.
  • J Offline
    J Offline
    Jost Pedro
    wrote on last edited by
    #1

    Hi guys, I have a class that extends an IMenuProvider interface and belongs to a plug-in project. In the UI project(other project) I've developed a menuService class that finds plug-ins that implement a IMenuProvider interface and execute the constructor method by using the invoke method. Here's the code.

    ...
    
     Type[] types = assembly.GetTypes();
                foreach (Type type in types)
                {
                    if (type.GetInterface("IMenuProvider") != null)
                    {
                        if (!ht.ContainsKey(type.FullName))
                        {
                            //get construct info.
                            ConstructorInfo cinfo = type.GetConstructor(System.Type.EmptyTypes);
                            
                            //invoke constructor 
                            IMenuProvider imp = (IMenuProvider)cinfo.Invoke(null);  **
                            ht.Add(type.FullName, imp);
                            RegisterProvider(imp);
                        }
                    }
                } 
    ...
    

    So what happens is the line marked with ** returns an error saying "Unable to cast object of type 'TestPlugin.PluginMenuProvider' to type 'Enki.UI.ComponentModel.IMenuProvider'." I know that I’m attempting to invoke the constructor method of the subclass PluginMenuProvider through the extension IMenuProvider but i don’t want to have to specify every subclass that extends the IMenuProvider interface. Now, my question is how can i invoke the constructor without having to declare and than cast each class type that extends the interface IMenuProvider like the line marked with ** does? Best regards. Pedro M.

    L M S 3 Replies Last reply
    0
    • J Jost Pedro

      Hi guys, I have a class that extends an IMenuProvider interface and belongs to a plug-in project. In the UI project(other project) I've developed a menuService class that finds plug-ins that implement a IMenuProvider interface and execute the constructor method by using the invoke method. Here's the code.

      ...
      
       Type[] types = assembly.GetTypes();
                  foreach (Type type in types)
                  {
                      if (type.GetInterface("IMenuProvider") != null)
                      {
                          if (!ht.ContainsKey(type.FullName))
                          {
                              //get construct info.
                              ConstructorInfo cinfo = type.GetConstructor(System.Type.EmptyTypes);
                              
                              //invoke constructor 
                              IMenuProvider imp = (IMenuProvider)cinfo.Invoke(null);  **
                              ht.Add(type.FullName, imp);
                              RegisterProvider(imp);
                          }
                      }
                  } 
      ...
      

      So what happens is the line marked with ** returns an error saying "Unable to cast object of type 'TestPlugin.PluginMenuProvider' to type 'Enki.UI.ComponentModel.IMenuProvider'." I know that I’m attempting to invoke the constructor method of the subclass PluginMenuProvider through the extension IMenuProvider but i don’t want to have to specify every subclass that extends the IMenuProvider interface. Now, my question is how can i invoke the constructor without having to declare and than cast each class type that extends the interface IMenuProvider like the line marked with ** does? Best regards. Pedro M.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Jost Pedro wrote:

      execute the constructor method by using the invoke method

      Invoking constructors might have a place but I would avoid it. From a design perspective I would use a Creational Design Pattern.

      led mike

      1 Reply Last reply
      0
      • J Jost Pedro

        Hi guys, I have a class that extends an IMenuProvider interface and belongs to a plug-in project. In the UI project(other project) I've developed a menuService class that finds plug-ins that implement a IMenuProvider interface and execute the constructor method by using the invoke method. Here's the code.

        ...
        
         Type[] types = assembly.GetTypes();
                    foreach (Type type in types)
                    {
                        if (type.GetInterface("IMenuProvider") != null)
                        {
                            if (!ht.ContainsKey(type.FullName))
                            {
                                //get construct info.
                                ConstructorInfo cinfo = type.GetConstructor(System.Type.EmptyTypes);
                                
                                //invoke constructor 
                                IMenuProvider imp = (IMenuProvider)cinfo.Invoke(null);  **
                                ht.Add(type.FullName, imp);
                                RegisterProvider(imp);
                            }
                        }
                    } 
        ...
        

        So what happens is the line marked with ** returns an error saying "Unable to cast object of type 'TestPlugin.PluginMenuProvider' to type 'Enki.UI.ComponentModel.IMenuProvider'." I know that I’m attempting to invoke the constructor method of the subclass PluginMenuProvider through the extension IMenuProvider but i don’t want to have to specify every subclass that extends the IMenuProvider interface. Now, my question is how can i invoke the constructor without having to declare and than cast each class type that extends the interface IMenuProvider like the line marked with ** does? Best regards. Pedro M.

        M Offline
        M Offline
        MGoettmann
        wrote on last edited by
        #3

        Pedro, have a look at Plug-ins and cast exceptions [^]. Martin

        1 Reply Last reply
        0
        • J Jost Pedro

          Hi guys, I have a class that extends an IMenuProvider interface and belongs to a plug-in project. In the UI project(other project) I've developed a menuService class that finds plug-ins that implement a IMenuProvider interface and execute the constructor method by using the invoke method. Here's the code.

          ...
          
           Type[] types = assembly.GetTypes();
                      foreach (Type type in types)
                      {
                          if (type.GetInterface("IMenuProvider") != null)
                          {
                              if (!ht.ContainsKey(type.FullName))
                              {
                                  //get construct info.
                                  ConstructorInfo cinfo = type.GetConstructor(System.Type.EmptyTypes);
                                  
                                  //invoke constructor 
                                  IMenuProvider imp = (IMenuProvider)cinfo.Invoke(null);  **
                                  ht.Add(type.FullName, imp);
                                  RegisterProvider(imp);
                              }
                          }
                      } 
          ...
          

          So what happens is the line marked with ** returns an error saying "Unable to cast object of type 'TestPlugin.PluginMenuProvider' to type 'Enki.UI.ComponentModel.IMenuProvider'." I know that I’m attempting to invoke the constructor method of the subclass PluginMenuProvider through the extension IMenuProvider but i don’t want to have to specify every subclass that extends the IMenuProvider interface. Now, my question is how can i invoke the constructor without having to declare and than cast each class type that extends the interface IMenuProvider like the line marked with ** does? Best regards. Pedro M.

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          How about simply using Activator.CreateInstace[^] instead, especially considering that the constructor doesn't take any parameters? That's the typical way in which objects are created using reflection.

          Type[] types = assembly.GetTypes();             foreach (Type type in types)             {                 if (type.GetInterface("IMenuProvider") != null)                 {                     if (!ht.ContainsKey(type.FullName))                     {                          IMenuProvider imp = (IMenuProvider)Activator.CreateInstance(type);
                                   ...

          Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | WinMacro

          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