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. Activator.Create instance - please save my sanity

Activator.Create instance - please save my sanity

Scheduled Pinned Locked Moved C#
helptutorialquestion
5 Posts 4 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.
  • N Offline
    N Offline
    Nathan Gloyn
    wrote on last edited by
    #1

    I'm using the following code to create an instance of a Type: Activator.CreateInstance(propertyType) // Where propertyType is a Type This seems to work fine for the majority of types but when I attempt to use it for a string I get the following error: No parameterless constructor defined for this object. I've been looking high and low for any examples or documentation on the how to do this and haven't been able to find anything. Can anybody help and save my sanity?

    J S G N 4 Replies Last reply
    0
    • N Nathan Gloyn

      I'm using the following code to create an instance of a Type: Activator.CreateInstance(propertyType) // Where propertyType is a Type This seems to work fine for the majority of types but when I attempt to use it for a string I get the following error: No parameterless constructor defined for this object. I've been looking high and low for any examples or documentation on the how to do this and haven't been able to find anything. Can anybody help and save my sanity?

      J Offline
      J Offline
      J a a n s
      wrote on last edited by
      #2

      Lowest of the Low wrote:

      No parameterless constructor defined for this object.

      There is no parameterless constructor for String[^] class. Activator.CreateInstance method requires one. You can use this CreateInstance(Type, object[]) overloaded method. Check this link http://msdn2.microsoft.com/en-us/library/system.activator.createinstance.aspx[^]

      *jaans

      1 Reply Last reply
      0
      • N Nathan Gloyn

        I'm using the following code to create an instance of a Type: Activator.CreateInstance(propertyType) // Where propertyType is a Type This seems to work fine for the majority of types but when I attempt to use it for a string I get the following error: No parameterless constructor defined for this object. I've been looking high and low for any examples or documentation on the how to do this and haven't been able to find anything. Can anybody help and save my sanity?

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

        Activator.Create instantiates an object using its default constructor. As you can see here[^], string has no default constructor. Activator.Create is probably identical to the following:

        public object CreateInstance(Type objType) {
        ConstructorInfo ci = objType.GetConstructor(Type.EmptyTypes);
        if (ci == null)
        throw new Exception("No parameterless constructor defined for this object.");
        return ci.Invoke(null);
        }

        You will need to have special cases for classes that do not implement a default constructor, either by extending the classes with extension methods (if you have .Net 3.0 or above), or by using a try/catch to catch the exception and instantiate those classes yourself. Hope this helps,

        Sounds like somebody's got a case of the Mondays -Jeff

        1 Reply Last reply
        0
        • N Nathan Gloyn

          I'm using the following code to create an instance of a Type: Activator.CreateInstance(propertyType) // Where propertyType is a Type This seems to work fine for the majority of types but when I attempt to use it for a string I get the following error: No parameterless constructor defined for this object. I've been looking high and low for any examples or documentation on the how to do this and haven't been able to find anything. Can anybody help and save my sanity?

          G Offline
          G Offline
          Giorgi Dalakishvili
          wrote on last edited by
          #4

          I guess the reason is that string class has not got public default constructor. You can try either using other overload of CreateInstance or create an instance of StringBuilder class.

          #region signature my articles #endregion

          1 Reply Last reply
          0
          • N Nathan Gloyn

            I'm using the following code to create an instance of a Type: Activator.CreateInstance(propertyType) // Where propertyType is a Type This seems to work fine for the majority of types but when I attempt to use it for a string I get the following error: No parameterless constructor defined for this object. I've been looking high and low for any examples or documentation on the how to do this and haven't been able to find anything. Can anybody help and save my sanity?

            N Offline
            N Offline
            Nathan Gloyn
            wrote on last edited by
            #5

            Just wanted to say thanks to everybody that posted. I came across this posting http://groups.google.co.uk/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/817b411162fa122/9ea3b34cc60bc2a2?hl=en&lnk=st&q=c%23+variables+default+values#9ea3b34cc60bc2a2[^] That goes into default values for variables and using the Activator.CreateInstance. From my set of limited tests so far String is the only Type that I've had any problems with.

            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