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 submit just a class name as a parameter of a method?

How to submit just a class name as a parameter of a method?

Scheduled Pinned Locked Moved C#
questiontutorial
8 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.
  • T Offline
    T Offline
    ThomasZ01
    wrote on last edited by
    #1

    Hello, I want to submit somehow a class name to a method and inside the method i would like to create multiple instances of the submitted class. The reason is that I want to call the constructor of a another class and want to tell which other class instances should be created. How can i do that?

    A A K P 4 Replies Last reply
    0
    • T ThomasZ01

      Hello, I want to submit somehow a class name to a method and inside the method i would like to create multiple instances of the submitted class. The reason is that I want to call the constructor of a another class and want to tell which other class instances should be created. How can i do that?

      A Offline
      A Offline
      AhsanS
      wrote on last edited by
      #2

      Create a public enum of say ClassTypes and pass it to the method. Apply switch to ClassTypes and generate classes as you may require. e.g public enum ClassType { ClassA, ClassB } classCInstance.CreateClass(ClassType objClass) { switch(objClass) { case ClassType.ClassA: new ClassA(); break; case ClassType.ClassB: new ClassB(); break; } } P.S: This is just an example for your understanding. Hope it helps Ahsan Ullah Senior Software Engineer MCTS 2.0

      T 1 Reply Last reply
      0
      • T ThomasZ01

        Hello, I want to submit somehow a class name to a method and inside the method i would like to create multiple instances of the submitted class. The reason is that I want to call the constructor of a another class and want to tell which other class instances should be created. How can i do that?

        A Offline
        A Offline
        AspDotNetDev
        wrote on last edited by
        #3

        C# reflection allows you to create instances of a type based on the classes string representation (i.e., the name of the class). Specifically, the article "Dynamically Loading and Using Types" should be of particular use to you.

        [Forum Guidelines]

        T 1 Reply Last reply
        0
        • A AspDotNetDev

          C# reflection allows you to create instances of a type based on the classes string representation (i.e., the name of the class). Specifically, the article "Dynamically Loading and Using Types" should be of particular use to you.

          [Forum Guidelines]

          T Offline
          T Offline
          ThomasZ01
          wrote on last edited by
          #4

          Thanks, that would be a solution, however it would force me to create a DLL. I would prefer to have those classes in my .exe.

          A 1 Reply Last reply
          0
          • A AhsanS

            Create a public enum of say ClassTypes and pass it to the method. Apply switch to ClassTypes and generate classes as you may require. e.g public enum ClassType { ClassA, ClassB } classCInstance.CreateClass(ClassType objClass) { switch(objClass) { case ClassType.ClassA: new ClassA(); break; case ClassType.ClassB: new ClassB(); break; } } P.S: This is just an example for your understanding. Hope it helps Ahsan Ullah Senior Software Engineer MCTS 2.0

            T Offline
            T Offline
            ThomasZ01
            wrote on last edited by
            #5

            Thanks, this looks good. The only disadvantage would be that I have to manage the enum and keep it up to date. I expect to have hundrets of different classes that would be listed in the enum. Would it be possible to get all the available classes? Basically I have those classes in a separate folder in visual studio and they are all derived from the same base class which could maybe used for the search.

            1 Reply Last reply
            0
            • T ThomasZ01

              Hello, I want to submit somehow a class name to a method and inside the method i would like to create multiple instances of the submitted class. The reason is that I want to call the constructor of a another class and want to tell which other class instances should be created. How can i do that?

              K Offline
              K Offline
              Karthik Kalyanasundaram
              wrote on last edited by
              #6

              I think, the Type class in .NET will help you. You can pass the type of the class to your function and create instances using Reflection.

              class YourClass
              {
              ......
              }

              .....

              public void YourMethod(Type type) { // do your stuffs here.. // you can use Reflection to create the instance. } public void AnotherMethod() { YourMethod(typeof(YourClass)); }

              Do more work Make more mistakes Learn more things

              1 Reply Last reply
              0
              • T ThomasZ01

                Thanks, that would be a solution, however it would force me to create a DLL. I would prefer to have those classes in my .exe.

                A Offline
                A Offline
                AspDotNetDev
                wrote on last edited by
                #7

                It doesn't need to be in a DLL to create the type. That's just what the example used. I'm pretty sure you can skip the step of loading the assembly altogether if the type is in the current assembly (i.e., if it is in the current EXE).

                [Forum Guidelines]

                1 Reply Last reply
                0
                • T ThomasZ01

                  Hello, I want to submit somehow a class name to a method and inside the method i would like to create multiple instances of the submitted class. The reason is that I want to call the constructor of a another class and want to tell which other class instances should be created. How can i do that?

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Or pass in a ConstructorInfo instead -- make the calling method do the Reflection.

                  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