Dynamically creating type of System.Int32, how??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Søren M. Olesen

    #1

    Dynamically creating type of System.Int32, how??

    Hi

    How do I dynamically create a type of System.Int32 with the value 123?? I've tried the following:

    Dim keyci As ConstructorInfo = Type.GetType("S ystem.Int32").G etConstructor(N ew Type() {Type.GetType(" System.Int32")} )
    Dim key As Object = keyci.Invoke(Ne w Object() {"123"})


    however it doesn't seem to work....

    TIA

    Søren

  • Herfried K. Wagner [MVP]

    #2
    Re: Dynamically creating type of System.Int32, how??

    "Søren M. Olesen" <smolesen@hotma il.com> schrieb:[color=blue]
    >How do I dynamically create a type of System.Int32 with the value 123??
    >I've tried the following:
    >
    >Dim keyci As ConstructorInfo =
    >Type.GetType(" System.Int32"). GetConstructor( New Type()
    >{Type.GetType( "System.Int32") })
    >Dim key As Object = keyci.Invoke(Ne w Object() {"123"})
    >
    >however it doesn't seem to work....[/color]

    .... because 'Int32' doesn't have a parameterized constructor.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Søren M. Olesen

      #3
      Re: Dynamically creating type of System.Int32, how??

      I kind of figured that, but is there a way to do it, or do I have to use a
      select case and then cast on the type??

      Søren



      "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
      news:OMmJTdClFH A.3336@tk2msftn gp13.phx.gbl...[color=blue]
      > "Søren M. Olesen" <smolesen@hotma il.com> schrieb:[color=green]
      >>How do I dynamically create a type of System.Int32 with the value 123??
      >>I've tried the following:
      >>
      >>Dim keyci As ConstructorInfo =
      >>Type.GetType( "System.Int32") .GetConstructor (New Type()
      >>{Type.GetType ("System.Int32" )})
      >>Dim key As Object = keyci.Invoke(Ne w Object() {"123"})
      >>
      >>however it doesn't seem to work....[/color]
      >
      > ... because 'Int32' doesn't have a parameterized constructor.
      >
      > --
      > M S Herfried K. Wagner
      > M V P <URL:http://dotnet.mvps.org/>
      > V B <URL:http://classicvb.org/petition/>[/color]


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Dynamically creating type of System.Int32, how??

        Søren,
        It appears you simply want to convert a String to an arbitrary type.

        Have you considered Convert.ChangeT ype?

        Something like:

        Dim key As Object = Convert.ChangeT ype("123",
        Type.GetType("S ystem.Int32"))

        Where the GetType changes based on outside criteria.

        However if you know you will always be using Int32, then a simply cast is
        easier:

        Either of the following:
        key = CInt("123")
        key = Convert.ToInt32 ("123")


        Hope this helps
        Jay

        "Søren M. Olesen" <smolesen@hotma il.com> wrote in message
        news:OXMkyjBlFH A.3096@TK2MSFTN GP10.phx.gbl...
        Hi

        How do I dynamically create a type of System.Int32 with the value 123?? I've
        tried the following:

        Dim keyci As ConstructorInfo =
        Type.GetType("S ystem.Int32").G etConstructor(N ew Type()
        {Type.GetType(" System.Int32")} )
        Dim key As Object = keyci.Invoke(Ne w Object() {"123"})


        however it doesn't seem to work....

        TIA

        Søren


        Comment

        Working...