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. Dynamically creating objects using reflection

Dynamically creating objects using reflection

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

    Hi All, I am trying to dynamically create object based on values that being submited from a form, but keep on getting a "No parameterless constructor defined for this object." error. Type paramType = Type.GetType("System." + Request["type"]); //The Type of object String paramValue = Request["val"]; //the value if (paramType != null) { Activator.CreateInstance(paramType); }

    P 1 Reply Last reply
    0
    • M mcd2424

      Hi All, I am trying to dynamically create object based on values that being submited from a form, but keep on getting a "No parameterless constructor defined for this object." error. Type paramType = Type.GetType("System." + Request["type"]); //The Type of object String paramValue = Request["val"]; //the value if (paramType != null) { Activator.CreateInstance(paramType); }

      P Offline
      P Offline
      Phillip M Hoff
      wrote on last edited by
      #2

      What are the types that you are trying to create, and do they all have public, parameterless constructors? -Phil

      M 1 Reply Last reply
      0
      • P Phillip M Hoff

        What are the types that you are trying to create, and do they all have public, parameterless constructors? -Phil

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

        Hi Phil, Right now I am just trying to create strings and ints, but will have to create other types of objects. All will be public but some will be parameterless constructors some will not. Thanks, tom

        P 1 Reply Last reply
        0
        • M mcd2424

          Hi Phil, Right now I am just trying to create strings and ints, but will have to create other types of objects. All will be public but some will be parameterless constructors some will not. Thanks, tom

          P Offline
          P Offline
          Phillip M Hoff
          wrote on last edited by
          #4

          The System.String type does not have a parameterless constructor (they all require at least one parameter), and therefore cannot be instantiated via the Activator.CreateInstance(Type) method. -Phil

          M 1 Reply Last reply
          0
          • P Phillip M Hoff

            The System.String type does not have a parameterless constructor (they all require at least one parameter), and therefore cannot be instantiated via the Activator.CreateInstance(Type) method. -Phil

            M Offline
            M Offline
            mcd2424
            wrote on last edited by
            #5

            thanks Phil, Would you mind showing me a quick example of how to create a string via reflection. This is what I tried using before but it allways returns null. Type[] ArgTypes = new Type[1] {Type.GetType("System.String") }; ConstructorInfo oConstructorInfo = paramType.GetConstructor(ArgTypes ); thanks

            P R 2 Replies Last reply
            0
            • M mcd2424

              thanks Phil, Would you mind showing me a quick example of how to create a string via reflection. This is what I tried using before but it allways returns null. Type[] ArgTypes = new Type[1] {Type.GetType("System.String") }; ConstructorInfo oConstructorInfo = paramType.GetConstructor(ArgTypes ); thanks

              P Offline
              P Offline
              Phillip M Hoff
              wrote on last edited by
              #6

              Here's one way to create the string "a" via reflection: Type type = Type.GetType("System.String"); object obj = Activator.CreateInstance(type, 'a', 1); In this case, the .NET runtime would use the System.String.String(char, int) constructor in order to create the instance because we passed a character and an integer to Activator.CreateInstance(). In the case of the .NET native types like strings and integers, however, if you already know what the value of the object will be, using reflection is overkill. There are far easier ways to translate between one native type and another. -Phil

              1 Reply Last reply
              0
              • M mcd2424

                thanks Phil, Would you mind showing me a quick example of how to create a string via reflection. This is what I tried using before but it allways returns null. Type[] ArgTypes = new Type[1] {Type.GetType("System.String") }; ConstructorInfo oConstructorInfo = paramType.GetConstructor(ArgTypes ); thanks

                R Offline
                R Offline
                Russell Jones
                wrote on last edited by
                #7

                You could Create a class called MyString add a parameterless constructor and an implicit cast public static implicit operator string (MyString A) { return A._internalStringVariable; } public static implicit operator MyString (String S) { return new MyString(S); } Then you'll have an object that behaves a bit like a string but has a parameterless constructor. HTH Russ

                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