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. creating dynamic classes

creating dynamic classes

Scheduled Pinned Locked Moved C#
questiondatabase
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.
  • M Offline
    M Offline
    Manu_81
    wrote on last edited by
    #1

    Hi, I am getting the name of the class from database as string. I want to create object for this class dynamically. how do I do it?

    G J R 3 Replies Last reply
    0
    • M Manu_81

      Hi, I am getting the name of the class from database as string. I want to create object for this class dynamically. how do I do it?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Create a Type object from the name. Use the GetConstructor method to get a ConstructorInfo object for the constructor. Use the Invoke method to run the constructor. --- b { font-weight: normal; }

      M 1 Reply Last reply
      0
      • G Guffa

        Create a Type object from the name. Use the GetConstructor method to get a ConstructorInfo object for the constructor. Use the Invoke method to run the constructor. --- b { font-weight: normal; }

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

        Please can you show a sample code.

        G 1 Reply Last reply
        0
        • M Manu_81

          Please can you show a sample code.

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          I explained exactly how to do it. Need I actually do it for you also? --- b { font-weight: normal; }

          K 1 Reply Last reply
          0
          • G Guffa

            I explained exactly how to do it. Need I actually do it for you also? --- b { font-weight: normal; }

            K Offline
            K Offline
            Karl 2000
            wrote on last edited by
            #5

            No need to be rude. Karl

            J 1 Reply Last reply
            0
            • K Karl 2000

              No need to be rude. Karl

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              That wasnt rude. It was simply "Give it a try yourself". Teach a man to fish and all that..... Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

              1 Reply Last reply
              0
              • M Manu_81

                Hi, I am getting the name of the class from database as string. I want to create object for this class dynamically. how do I do it?

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #7

                As well as the answer already provided, which will work no problem, you can also look at the CreateInstance method on the Activator class. The documentation can be found on msdn. Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

                1 Reply Last reply
                0
                • M Manu_81

                  Hi, I am getting the name of the class from database as string. I want to create object for this class dynamically. how do I do it?

                  R Offline
                  R Offline
                  Rob Philpott
                  wrote on last edited by
                  #8

                  Hi, As well as the class name, you need to know what assembly the class is contained in. There are different ways of doing this, but just lifting a bit of code from work:

                  Assembly assembly = Assembly.LoadFile(@"c:\myassembly.dll");
                  object dynamicClass = assembly.CreateInstance(classNameFromDb);

                  Not sure that helps as you end up with an untyped object. The way we do it here is to make the objects we create dynamically implement an interface, so the second line actually looks more like

                  IDynamicObject dynamicClass = assembly.CreateInstance(classNameFromDb) as IDynamicObject;

                  This way, we can then call the methods the interface specifies. After all, creating an instance of a class is little use if you have no idea what the object does. This is why defining an interface is useful. And don't forget the gotcha, the trap I always fall in with this, its not just the type name you need, but also the namespace. eg. "MyNamespace.MyClass" which you need to pass to CreateInstance. Regards, Rob Philpott.

                  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