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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. array type at runtime

array type at runtime

Scheduled Pinned Locked Moved C#
data-structuresquestion
9 Posts 2 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.
  • T Offline
    T Offline
    thepersonof
    wrote on last edited by
    #1

    hi I want to be able to do the following: Type mytype = MethodInfoArray[i].GetParameters()[0].GetType(); mytype[] p = new mytype[5]; Is that possible? cheers

    S 1 Reply Last reply
    0
    • T thepersonof

      hi I want to be able to do the following: Type mytype = MethodInfoArray[i].GetParameters()[0].GetType(); mytype[] p = new mytype[5]; Is that possible? cheers

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      You are looking for Array.CreateInstance[^]. You can use it like

      Type mytype = MethodInfoArray[i].GetParameters()[0].GetType();
      Array a = Array.CreateInstance(myType, 5);

      Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      T 1 Reply Last reply
      0
      • S S Senthil Kumar

        You are looking for Array.CreateInstance[^]. You can use it like

        Type mytype = MethodInfoArray[i].GetParameters()[0].GetType();
        Array a = Array.CreateInstance(myType, 5);

        Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        T Offline
        T Offline
        thepersonof
        wrote on last edited by
        #3

        Thank you ... that worked great ... but I should hae explained The bacis problem I have is this: Type requiredtype = getType(); Object[] datafrominputs = getData(); //I need to convert the array to the correct type (requiredtype) to pass as a parameter MethodInfoArray.Invoke(new Object(),new Object[]{datafrominputs}); Basically the method will take a single array as its parameter, but I need to be able to convert may Object array to the right type. What should I do? cheers -- modified at 7:30 Monday 29th May, 2006

        S 1 Reply Last reply
        0
        • T thepersonof

          Thank you ... that worked great ... but I should hae explained The bacis problem I have is this: Type requiredtype = getType(); Object[] datafrominputs = getData(); //I need to convert the array to the correct type (requiredtype) to pass as a parameter MethodInfoArray.Invoke(new Object(),new Object[]{datafrominputs}); Basically the method will take a single array as its parameter, but I need to be able to convert may Object array to the right type. What should I do? cheers -- modified at 7:30 Monday 29th May, 2006

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          Did you try Convert.ChangeType[^]?

          Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          T 1 Reply Last reply
          0
          • S S Senthil Kumar

            Did you try Convert.ChangeType[^]?

            Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            T Offline
            T Offline
            thepersonof
            wrote on last edited by
            #5

            Yeah I have but I get: InvalidCastException: "Object must implement IConvertable" I was trying to convert from an Object[] to an int[]. cheers

            S 1 Reply Last reply
            0
            • T thepersonof

              Yeah I have but I get: InvalidCastException: "Object must implement IConvertable" I was trying to convert from an Object[] to an int[]. cheers

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              Did you call Convert.ChangeType on the array itself or did you loop through the array and call it for each element in the array? I think the first approach won't work.

              Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              T 1 Reply Last reply
              0
              • S S Senthil Kumar

                Did you call Convert.ChangeType on the array itself or did you loop through the array and call it for each element in the array? I think the first approach won't work.

                Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                T Offline
                T Offline
                thepersonof
                wrote on last edited by
                #7

                y that's an idea... when looping through I would have to store the array in type determined at runtime (using the array class, I think). How could I then pass that as a parameter? thanks for all the help

                S 1 Reply Last reply
                0
                • T thepersonof

                  y that's an idea... when looping through I would have to store the array in type determined at runtime (using the array class, I think). How could I then pass that as a parameter? thanks for all the help

                  S Offline
                  S Offline
                  S Senthil Kumar
                  wrote on last edited by
                  #8

                  Try this piece of code

                  static Array ConvertArray(object[] arr, Type type)
                  {
                  Array a = Array.CreateInstance(type, arr.Length);
                  for (int i = 0; i

                  Regards
                  Senthil

                  Regards
                  Senthil
                  _____________________________
                  My Blog | My Articles | WinMacro

                  T 1 Reply Last reply
                  0
                  • S S Senthil Kumar

                    Try this piece of code

                    static Array ConvertArray(object[] arr, Type type)
                    {
                    Array a = Array.CreateInstance(type, arr.Length);
                    for (int i = 0; i

                    Regards
                    Senthil

                    Regards
                    Senthil
                    _____________________________
                    My Blog | My Articles | WinMacro

                    T Offline
                    T Offline
                    thepersonof
                    wrote on last edited by
                    #9

                    Thank you!!!

                    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