getting container element type with reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • colin

    getting container element type with reflection

    Hi,
    Im using reflection and I have a general handler for a container type
    object where I have the System.Type info of the container itself
    but I need to find out the type of the element contained in it.

    The full name of the Type info does usualy include the type,
    but is there a way of getting this without parsing the fullname ?

    the container type maybe a Byte[] or a class <maybe with a templatewich
    implements ICollection.

    I need to fill the container from a file.

    the element can be anything from a Byte to a packed binary struct
    or another class, or more awkwardlky a struct wich cant be bit copied.

    the size is either stored before the contents or is derived perviously.

    Colin =^.^=


  • Marc Gravell

    #2
    Re: getting container element type with reflection

    For arrays, Type.GetElement Type() should work; however, for
    collections you may need to check for IList<Tand then infer the T.

    Marc


    Comment

    • colin

      #3
      Re: getting container element type with reflection

      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:ecaG6GsKIH A.4196@TK2MSFTN GP04.phx.gbl...
      For arrays, Type.GetElement Type() should work; however, for collections
      you may need to check for IList<Tand then infer the T.
      thanks, thats helpfull for arrays,

      im not sure what you mean by infer the T
      I dont know what T is so im not sure how I look for IList<Twithout knowing
      before hand
      unless I just try all the known posibilities of T?
      this would only work for primitive types though, not for classes.

      Colin =^.^=


      Comment

      • Marc Gravell

        #4
        Re: getting container element type with reflection

        Like so:

        Type unknownType = typeof(List<str ing>); // pretend we
        don't know this ;-p
        foreach (Type intType in unknownType.Get Interfaces()) {
        if (intType.IsGene ricType &&
        intType.GetGene ricTypeDefiniti on()
        == typeof(IList<>) ) {
        Type elementType =
        intType.GetGene ricArguments()[0]; // ****
        Trace.WriteLine ("Implements IList<Tfor T = " +
        elementType.Ful lName);
        }
        }

        At the line marked "****" we have the element-type.

        Marc


        Comment

        • colin

          #5
          Re: getting container element type with reflection


          "Marc Gravell" <marc.gravell@g mail.comwrote in message
          news:eTKcuUsKIH A.280@TK2MSFTNG P03.phx.gbl...
          Like so:
          >
          Type unknownType = typeof(List<str ing>); // pretend we don't
          know this ;-p
          foreach (Type intType in unknownType.Get Interfaces()) {
          if (intType.IsGene ricType &&
          intType.GetGene ricTypeDefiniti on()
          == typeof(IList<>) ) {
          Type elementType = intType.GetGene ricArguments()[0]; //
          ****
          Trace.WriteLine ("Implements IList<Tfor T = " +
          elementType.Ful lName);
          }
          }
          >
          At the line marked "****" we have the element-type.
          >
          Marc
          cool thanks, I didnt understand it at first,
          but i tried it and although I had only implmented the non generic
          collection in my wrapper, just adding the IList<T>
          and letting it implement it with defualt members
          it finds the type of 'T' :D

          Colin =^.^=


          Comment

          • Marc Gravell

            #6
            Re: getting container element type with reflection

            I didnt understand it at first
            Sorry, I was in a hurry to run for a train, so didn't have time to add
            comments... let me know if you want a run-through...

            Comment

            • colin

              #7
              Re: getting container element type with reflection

              "Marc Gravell" <marc.gravell@g mail.comwrote in message
              news:d9d809b2-d87b-4e6d-8566-ff83e26b9eb1@i3 7g2000hsd.googl egroups.com...
              >I didnt understand it at first
              Sorry, I was in a hurry to run for a train, so didn't have time to add
              comments... let me know if you want a run-through...
              no thats fine thanks very much :)

              I just hadnt got to grips with stuff that says generic in it till now.

              I seem to have jumped in at the deep end using reflection on my first c# app
              lol.

              Colin =^.^=


              Comment

              • Marc Gravell

                #8
                Re: getting container element type with reflection

                Just remember: reflection is a valid option, but it should not be
                your /default/ option - quite the opposite: it is there for when you
                cannot *possibly* know anything about the types at compile time. MOst
                of the time there are often far more graceful ways to do the same
                thing. Just don't get into the habit of using it ;-p

                Happy coding,

                Marc

                Comment

                • colin

                  #9
                  Re: getting container element type with reflection

                  "Marc Gravell" <marc.gravell@g mail.comwrote in message
                  news:aa8baae9-b5f1-4a03-935d-bf58f7da9f8c@p6 9g2000hsa.googl egroups.com...
                  Just remember: reflection is a valid option, but it should not be
                  your /default/ option - quite the opposite: it is there for when you
                  cannot *possibly* know anything about the types at compile time. MOst
                  of the time there are often far more graceful ways to do the same
                  thing. Just don't get into the habit of using it ;-p
                  yeah, I knew I had a lot of structures to import from an old
                  file format, and they were big too, and also subject to user modification
                  c# seemed to offer quite an attractive route, its not that slow either,
                  has been quite a hard learning curve though.

                  its also been usefull for dumping the data structures for debug too.
                  it would be a long chore to write serialise in,out, and ToString
                  for every structure.

                  reflection just breezes through it all now.

                  for my next trick im going to tackle user defined types lol.
                  although I may try and avoid it and do the user script stuff some other way,
                  or just avoid it altogether. for now the user defined fields are treated
                  like
                  database fields.

                  Colin =^.^=


                  Comment

                  Working...