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 an instance of type using string litteral

Creating an instance of type using string litteral

Scheduled Pinned Locked Moved C#
tutorialquestion
7 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.
  • V Offline
    V Offline
    Venkatraman
    wrote on last edited by
    #1

    Hey, I have read this before it is possible to create an instance of a type by specifying their string literal equivalent. for example if i need to create an instance of Employee class generally i will say Employee objEmployee = new Employee(); my question is i just know that the class which i need to instantiate is "Employee" how will i instantiate using the string "Employee" hope i did not made the simple ques more complex. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)

    R K S C 4 Replies Last reply
    0
    • V Venkatraman

      Hey, I have read this before it is possible to create an instance of a type by specifying their string literal equivalent. for example if i need to create an instance of Employee class generally i will say Employee objEmployee = new Employee(); my question is i just know that the class which i need to instantiate is "Employee" how will i instantiate using the string "Employee" hope i did not made the simple ques more complex. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)

      R Offline
      R Offline
      Rocky Moore
      wrote on last edited by
      #2

      Check out AppDomain.CreateInstance and AppDomain.CreateInstanceFrom Rocky Moore <><

      1 Reply Last reply
      0
      • V Venkatraman

        Hey, I have read this before it is possible to create an instance of a type by specifying their string literal equivalent. for example if i need to create an instance of Employee class generally i will say Employee objEmployee = new Employee(); my question is i just know that the class which i need to instantiate is "Employee" how will i instantiate using the string "Employee" hope i did not made the simple ques more complex. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)

        S Offline
        S Offline
        shaunAustin
        wrote on last edited by
        #3

        You can instantiate a class using a "partial" name... i.e. the type name as a string literal, one way is... string path = "MyApp.MyAssembly"; // assembly name string class = "MyClass"; // class in the assembly object MyObj = Assembly.Load(path).CreateInstance(class); This is most useful when your class implements a known interface but you want to dynamically load the implementation because you can do the following. IFooBar fooBar = Assembly.Load(path).CreateInstance(class) as IFooBar This stuff is my favourite thing about .net... :-D :cool: HTH Shaun :-D ----------------------------------------------------------------------- Shaun Austin: .NET Specialist. Spreading the word of .NET to the world... well the UK... well my tiny corner of it!! :-D

        V 1 Reply Last reply
        0
        • V Venkatraman

          Hey, I have read this before it is possible to create an instance of a type by specifying their string literal equivalent. for example if i need to create an instance of Employee class generally i will say Employee objEmployee = new Employee(); my question is i just know that the class which i need to instantiate is "Employee" how will i instantiate using the string "Employee" hope i did not made the simple ques more complex. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)

          K Offline
          K Offline
          Kannan Kalyanaraman
          wrote on last edited by
          #4

          Venkat, Have a look at CreateInstance and CreateInstanceAndUnwrap method, I think you can also do this using other reflection techniques. Cheers, Kannan

          V 1 Reply Last reply
          0
          • K Kannan Kalyanaraman

            Venkat, Have a look at CreateInstance and CreateInstanceAndUnwrap method, I think you can also do this using other reflection techniques. Cheers, Kannan

            V Offline
            V Offline
            Venkatraman
            wrote on last edited by
            #5

            thanks for your info Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)

            1 Reply Last reply
            0
            • S shaunAustin

              You can instantiate a class using a "partial" name... i.e. the type name as a string literal, one way is... string path = "MyApp.MyAssembly"; // assembly name string class = "MyClass"; // class in the assembly object MyObj = Assembly.Load(path).CreateInstance(class); This is most useful when your class implements a known interface but you want to dynamically load the implementation because you can do the following. IFooBar fooBar = Assembly.Load(path).CreateInstance(class) as IFooBar This stuff is my favourite thing about .net... :-D :cool: HTH Shaun :-D ----------------------------------------------------------------------- Shaun Austin: .NET Specialist. Spreading the word of .NET to the world... well the UK... well my tiny corner of it!! :-D

              V Offline
              V Offline
              Venkatraman
              wrote on last edited by
              #6

              thanks for your info Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)

              1 Reply Last reply
              0
              • V Venkatraman

                Hey, I have read this before it is possible to create an instance of a type by specifying their string literal equivalent. for example if i need to create an instance of Employee class generally i will say Employee objEmployee = new Employee(); my question is i just know that the class which i need to instantiate is "Employee" how will i instantiate using the string "Employee" hope i did not made the simple ques more complex. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)

                C Offline
                C Offline
                CBoland
                wrote on last edited by
                #7

                You could also use Activator.CreateInstance. This is much simpler that using AppDomain.CreateInstance, especially if the "Employee" class is in the same assembly. It also returns an Object instance instead of an ObjectHandle.

                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