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. Can I create a class from a string name??

Can I create a class from a string name??

Scheduled Pinned Locked Moved C#
csharptutorialquestion
4 Posts 3 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.
  • S Offline
    S Offline
    Saeed 394
    wrote on last edited by
    #1

    Can I create a class from a string name?? I want to Make a new instanc class from string name in C#. for example: string nameClass = "Class1"; Class1 { .... } nameClass newInstance = new nameClass(); Thanks For Your Time.

    R S 2 Replies Last reply
    0
    • S Saeed 394

      Can I create a class from a string name?? I want to Make a new instanc class from string name in C#. for example: string nameClass = "Class1"; Class1 { .... } nameClass newInstance = new nameClass(); Thanks For Your Time.

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      See the Activator[^] class. /ravi

      This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      S 1 Reply Last reply
      0
      • R Ravi Bhavnani

        See the Activator[^] class. /ravi

        This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

        S Offline
        S Offline
        sarvesh upadhyay
        wrote on last edited by
        #3

        Yes we can using Reflection. 1. suppose you have added reference of a class 'clsTest' in your project. 2. then add namespace System.Reflection using System.Reflection; now in your code write Type tpClsTest = Type.GetType("clsTest"); object objTest = Activator.CreateInstance(tpClsTest); now objTest represents the instance of your clsTest. sarvesh C

        1 Reply Last reply
        0
        • S Saeed 394

          Can I create a class from a string name?? I want to Make a new instanc class from string name in C#. for example: string nameClass = "Class1"; Class1 { .... } nameClass newInstance = new nameClass(); Thanks For Your Time.

          S Offline
          S Offline
          Saeed 394
          wrote on last edited by
          #4

          Hi.Thanks Can you Write Example Code For me?? I cant Run that method for my aim. my aim: string nameClass = "Class1"; Class1 { .... } nameClass newInstance = new nameClass(); how can i write this code with activator?? best regards.

          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