Reading a struct, field by field

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

    Reading a struct, field by field

    Is there a way to read a struct field by field?

    I want to write a rather general function, like:

    public string EncodeStruct (struct strct)
    {
    ....
    }

    which accepts a general struct type, reads it field by field, encodes
    each field depending on its type (i.e. an integer field is encoding is
    different than a boolean), concatenates the results into a string and
    returns it.

  • Jon Skeet [C# MVP]

    #2
    Re: Reading a struct, field by field

    Sadeq wrote:[color=blue]
    > Is there a way to read a struct field by field?
    >
    > I want to write a rather general function, like:
    >
    > public string EncodeStruct (struct strct)
    > {
    > ...
    > }
    >
    > which accepts a general struct type, reads it field by field, encodes
    > each field depending on its type (i.e. an integer field is encoding is
    > different than a boolean), concatenates the results into a string and
    > returns it.[/color]

    Have a look at Type.GetFields.

    Note that you can't have a method signature like the one you've got at
    the moment. If you're using .NET 2.0, you can do something like:

    public string EncodeStruct<T> (T value) where T : struct
    {
    Type type = typeof(T);
    ...
    }

    If you're using 1.1, you'll probably need to take the hit of boxing the
    value and make the signature of the method just EncodeStruct(ob ject
    value).

    Jon

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Reading a struct, field by field

      Jon Skeet [C# MVP] wrote:

      <snip>
      [color=blue]
      > Note that you can't have a method signature like the one you've got at
      > the moment. If you're using .NET 2.0, you can do something like:
      >
      > public string EncodeStruct<T> (T value) where T : struct
      > {
      > Type type = typeof(T);
      > ...
      > }
      >
      > If you're using 1.1, you'll probably need to take the hit of boxing the
      > value and make the signature of the method just EncodeStruct(ob ject
      > value).[/color]

      Actually, thinking about it further, you'll have to take the hit of
      boxing at least once anyway, as FieldInfo.GetVa lue takes object as a
      parameter.

      Jon

      Comment

      • Sadeq

        #4
        Re: Reading a struct, field by field

        Thanks a lot. That was of great help.

        I used .NET 2.0 syntax, and now I'm able to implement my own encoder.

        But another issue arose when I wanted to encode nested structs. I tried
        to use it recursively, but I the compiler cannot cast the obtained
        field (which is an object, in general) to a struct.

        Is there a way to overcome this issue?

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Reading a struct, field by field

          Sadeq <MSDousti@gmail .com> wrote:[color=blue]
          > Thanks a lot. That was of great help.
          >
          > I used .NET 2.0 syntax, and now I'm able to implement my own encoder.
          >
          > But another issue arose when I wanted to encode nested structs. I tried
          > to use it recursively, but I the compiler cannot cast the obtained
          > field (which is an object, in general) to a struct.
          >
          > Is there a way to overcome this issue?[/color]

          Well, why not just use it in its boxed form? As I said elsewhere, I
          don't think you'll be able to get round it being boxed at some stage
          anyway - you might as well take the hit once and be done with it.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Sadeq

            #6
            Re: Reading a struct, field by field

            So, there's no way to unbox an object (a boxed struct) back to the
            struct?

            And another thing: Is there a way to know if an object is in fact a
            boxed struct?

            Comment

            • William Stacey [MVP]

              #7
              Re: Reading a struct, field by field

              Why not just use binaryserialize r? Serialize to byte[], then return base64
              string of that?

              --
              William Stacey [MVP]

              "Sadeq" <MSDousti@gmail .com> wrote in message
              news:1138268511 .826563.56320@g 44g2000cwa.goog legroups.com...
              | Is there a way to read a struct field by field?
              |
              | I want to write a rather general function, like:
              |
              | public string EncodeStruct (struct strct)
              | {
              | ...
              | }
              |
              | which accepts a general struct type, reads it field by field, encodes
              | each field depending on its type (i.e. an integer field is encoding is
              | different than a boolean), concatenates the results into a string and
              | returns it.
              |


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Reading a struct, field by field

                Sadeq <MSDousti@gmail .com> wrote:[color=blue]
                > So, there's no way to unbox an object (a boxed struct) back to the
                > struct?[/color]

                Yes - just cast it.
                [color=blue]
                > And another thing: Is there a way to know if an object is in fact a
                > boxed struct?[/color]

                You can call GetType(), and then use IsValueType() on the returned
                Type.

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Sadeq

                  #9
                  Re: Reading a struct, field by field

                  Hi
                  Sorry, I was busy with a project, so I checked the answers a bit late
                  ;) And b4 everything, thanx 4 ur answers.

                  I think I should clarify my problem a bit more: I want to write a
                  general function, which takes a struct (any type which is of type
                  struct is acceptable) and encodes into a string. This encoding is a
                  special, so I connot use .NET Serializiation.

                  First of all, with Jon Skeet's help, I wrote the function as follows:

                  public String encodeStruct<T> (T sequence) where T:struct
                  {
                  Type type;
                  StringBuilder sb = new StringBuilder() ;
                  FieldInfo[] fields = sequence.GetTyp e().GetFields() ;
                  foreach (FieldInfo f in fields)
                  {
                  type = f.FieldType;
                  if (type == typeof(int))
                  sb.Append(encod eInteger((int)f .GetValue(seque nce)));
                  else if (type == typeof(DateTime ))
                  sb.Append(encod eUTCTime((DateT ime)f.GetValue( sequence)));
                  else if (type == typeof(Boolean) )
                  sb.Append(encod eBoolean((Boole an)f.GetValue(s equence)));

                  Comment

                  • Nick Hounsome

                    #10
                    Re: Reading a struct, field by field


                    "Sadeq" <MSDousti@gmail .com> wrote in message
                    news:1138557856 .132930.247150@ g47g2000cwa.goo glegroups.com.. .[color=blue]
                    > Hi
                    > Sorry, I was busy with a project, so I checked the answers a bit late
                    > ;) And b4 everything, thanx 4 ur answers.
                    >
                    > I think I should clarify my problem a bit more: I want to write a
                    > general function, which takes a struct (any type which is of type
                    > struct is acceptable) and encodes into a string. This encoding is a
                    > special, so I connot use .NET Serializiation.
                    >
                    > First of all, with Jon Skeet's help, I wrote the function as follows:
                    >
                    > public String encodeStruct<T> (T sequence) where T:struct
                    > {
                    > Type type;
                    > StringBuilder sb = new StringBuilder() ;
                    > FieldInfo[] fields = sequence.GetTyp e().GetFields() ;
                    > foreach (FieldInfo f in fields)
                    > {
                    > type = f.FieldType;
                    > if (type == typeof(int))
                    > sb.Append(encod eInteger((int)f .GetValue(seque nce)));
                    > else if (type == typeof(DateTime ))
                    > sb.Append(encod eUTCTime((DateT ime)f.GetValue( sequence)));
                    > else if (type == typeof(Boolean) )
                    > sb.Append(encod eBoolean((Boole an)f.GetValue(s equence)));
                    > .
                    > .
                    > .
                    > .
                    > .
                    > }
                    > return sb.ToString();
                    > }
                    >
                    > You see, I implemented my own encoders for many types, like Integer,
                    > DateTime, Boolean, etc.
                    >
                    > Everything was good till I encountered a case in which I should encode
                    > nested structs, i.e. there was a struct within another one. Note that
                    > the inner struct can be general too, so I did't know in advance what
                    > kind of struct I should cast it to. First I tried something like this
                    > (Calling encodeStruct recursively):
                    >
                    > if(type == typeof(struct))
                    > sb.Append(encod eStruct((struct )f.GetValue(seq uence)));
                    >
                    > But it doesn't work for 2 reasons:
                    > 1- typeof(struct) returns error.
                    > 2- cast to "struct" returns error.
                    >
                    > I solved the first issue using "IsValueTyp e()" function, Thanx to Jon
                    > Skeet.
                    >
                    > To solve the second issue, I changed the function signature so that it
                    > accepts an object instead of the generic T, and there was no problem.
                    > But I was wondering if I can cast a general boxed struct [in this case,
                    > f.GetValue(sequ ence)] to a struct so that I don't have to lose this
                    > good feature of .NET 2.0, namely generics (You know, they apply
                    > compile-time type checking, have better performance, etc).
                    >[/color]

                    There are two ways to go here:

                    1) Semi-generic: Use a generic wrapper to ensure that it is only called from
                    USER code for structs. Have this wrapper call an internal, non-generic,
                    object version (which is recursive).

                    2) The XmlSerializer approach: construct a fully typesafe generic serializer
                    dynamically.

                    option 2 doesn't really buy you anything apart from efficiency because the
                    serializer creation method is trying to do much the same sort of operation
                    as the encodeStruct method.

                    In short - generics and reflection can never be mixed in the way you want
                    because using reflection necessarily means that you haven't got the compile
                    time types that are needed for generics.

                    P.S. Just thought of optuion 3: call encodeStruct<T> dynamically. It would
                    work but what is the point of calling a typesafe method in a non-typesafe
                    way.



                    Comment

                    • Sadeq

                      #11
                      Re: Reading a struct, field by field

                      Can you tell me about your first way (Semi-generic) more? I don't have
                      an idea of how to implement it. Do you mean I should create a generic
                      class like this:

                      class sequence<T> { ... }

                      and use it instead of "struct" type?


                      Nick Hounsome wrote:[color=blue]
                      > There are two ways to go here:
                      >
                      > 1) Semi-generic: Use a generic wrapper to ensure that it is only called from
                      > USER code for structs. Have this wrapper call an internal, non-generic,
                      > object version (which is recursive).
                      >
                      > 2) The XmlSerializer approach: construct a fully typesafe generic serializer
                      > dynamically.
                      >
                      > option 2 doesn't really buy you anything apart from efficiency because the
                      > serializer creation method is trying to do much the same sort of operation
                      > as the encodeStruct method.
                      >
                      > In short - generics and reflection can never be mixed in the way you want
                      > because using reflection necessarily means that you haven't got the compile
                      > time types that are needed for generics.
                      >
                      > P.S. Just thought of optuion 3: call encodeStruct<T> dynamically. It would
                      > work but what is the point of calling a typesafe method in a non-typesafe
                      > way.[/color]

                      Comment

                      • Nick Hounsome

                        #12
                        Re: Reading a struct, field by field

                        Sorry - I didn't really think it through.
                        I was just thinking:

                        public String encodeStruct<T> (T sequence) where T:struct
                        {
                        return realEncode(sequ ence);
                        }

                        private string realEncode(Obje ct sequence)
                        {
                        /* The (recursive) guts of your existing method */
                        }

                        But this gives you nothing extra.

                        I would suggest that you retain the generic because it saves one level of
                        boxing and enforces that you only call it with a value type - however the
                        code has to be as if the parameter was always Object (or ValueType) so maybe
                        just using encodeStruct(Va lueType sequence) is the cleanest method.

                        The fact remains that generics and reflection don't really mix well and the
                        reasons relate to the concepts themselves rather than the language.


                        "Sadeq" <MSDousti@gmail .com> wrote in message
                        news:1138616837 .396717.238620@ g43g2000cwa.goo glegroups.com.. .[color=blue]
                        > Can you tell me about your first way (Semi-generic) more? I don't have
                        > an idea of how to implement it. Do you mean I should create a generic
                        > class like this:
                        >
                        > class sequence<T> { ... }
                        >
                        > and use it instead of "struct" type?
                        >
                        >
                        > Nick Hounsome wrote:[color=green]
                        >> There are two ways to go here:
                        >>
                        >> 1) Semi-generic: Use a generic wrapper to ensure that it is only called
                        >> from
                        >> USER code for structs. Have this wrapper call an internal, non-generic,
                        >> object version (which is recursive).
                        >>
                        >> 2) The XmlSerializer approach: construct a fully typesafe generic
                        >> serializer
                        >> dynamically.
                        >>
                        >> option 2 doesn't really buy you anything apart from efficiency because
                        >> the
                        >> serializer creation method is trying to do much the same sort of
                        >> operation
                        >> as the encodeStruct method.
                        >>
                        >> In short - generics and reflection can never be mixed in the way you want
                        >> because using reflection necessarily means that you haven't got the
                        >> compile
                        >> time types that are needed for generics.
                        >>
                        >> P.S. Just thought of optuion 3: call encodeStruct<T> dynamically. It
                        >> would
                        >> work but what is the point of calling a typesafe method in a non-typesafe
                        >> way.[/color]
                        >[/color]


                        Comment

                        • Sadeq

                          #13
                          Re: Reading a struct, field by field

                          Thanx. I think this semi-grneric is better than no generics at all.

                          Of course the reflection and generics concepts do not mix well, but if
                          microsoft invented a way to "JUST" let the users cast to a general
                          "struct", there were no problems. I think this can be done without
                          mixing compile time and runtime concepts. Don't u think so?

                          Comment

                          Working...