Reflection with array

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

    Reflection with array

    I want to deserialize a file. In one ofe my classes I have an array
    like:

    public class MyClass
    {
    public MyArray[] theArray;
    }

    i.e. an arry of an own defined class. While reading the file I only know
    the dimension on the end of reading, so I keep the array elements in an
    ArrayList "records". At the end I "only" have to assign the ArrayList to
    the Array-field "theArray". And there is my problem.

    arrayField.Fiel dType shows "MyArray[]", this is correct for me. If I try
    with

    arrayField.SetV alue(records.To Array(), instanceOfMyCla ss);

    to assign the Array I get the error message that the objecttype can't be
    converted to the destination type.
    That's right, the object type is "object[]" and the destination type is
    "MyArray[]".

    How can I do the typecast at runtime?
    Or is there another way to do this?

    --
    Sleepless in Berlin
    Wernfried
  • Stoitcho Goutsev \(100\) [C# MVP]

    #2
    Re: Reflection with array

    Hi Wernfried,

    Before digging more deeply into this.

    Are you sure that the invalid cast is because of the arrays
    [color=blue]
    > arrayField.SetV alue(records.To Array(), instanceOfMyCla ss);[/color]

    AFIK in FiledInfo.SetVa lue first comes the object which field you set and
    then the value of the field. So I believe you should switch the paramers

    arrayField.SetV alue(instanceOf MyClass, records.ToArray ());

    Try that and tell if the probles is still there

    --
    HTH
    Stoitcho Goutsev (100) [C# MVP]


    "Wernfried Schwenkner" <wernfried.nosp am.schwenkner@s esa.de> wrote in
    message news:ol1ebc.6hh .ln@news.bln.se sa.de...[color=blue]
    > I want to deserialize a file. In one ofe my classes I have an array
    > like:
    >
    > public class MyClass
    > {
    > public MyArray[] theArray;
    > }
    >
    > i.e. an arry of an own defined class. While reading the file I only know
    > the dimension on the end of reading, so I keep the array elements in an
    > ArrayList "records". At the end I "only" have to assign the ArrayList to
    > the Array-field "theArray". And there is my problem.
    >
    > arrayField.Fiel dType shows "MyArray[]", this is correct for me. If I try
    > with
    >
    > arrayField.SetV alue(records.To Array(), instanceOfMyCla ss);
    >
    > to assign the Array I get the error message that the objecttype can't be
    > converted to the destination type.
    > That's right, the object type is "object[]" and the destination type is
    > "MyArray[]".
    >
    > How can I do the typecast at runtime?
    > Or is there another way to do this?
    >
    > --
    > Sleepless in Berlin
    > Wernfried[/color]


    Comment

    • Stoitcho Goutsev \(100\) [C# MVP]

      #3
      Re: Reflection with array

      Hi Wernfried,

      Before digging more deeply into this.

      Are you sure that the invalid cast is because of the arrays
      [color=blue]
      > arrayField.SetV alue(records.To Array(), instanceOfMyCla ss);[/color]

      AFIK in FiledInfo.SetVa lue first comes the object which field you set and
      then the value of the field. So I believe you should switch the paramers

      arrayField.SetV alue(instanceOf MyClass, records.ToArray ());

      Try that and tell if the probles is still there

      --
      HTH
      Stoitcho Goutsev (100) [C# MVP]


      "Wernfried Schwenkner" <wernfried.nosp am.schwenkner@s esa.de> wrote in
      message news:ol1ebc.6hh .ln@news.bln.se sa.de...[color=blue]
      > I want to deserialize a file. In one ofe my classes I have an array
      > like:
      >
      > public class MyClass
      > {
      > public MyArray[] theArray;
      > }
      >
      > i.e. an arry of an own defined class. While reading the file I only know
      > the dimension on the end of reading, so I keep the array elements in an
      > ArrayList "records". At the end I "only" have to assign the ArrayList to
      > the Array-field "theArray". And there is my problem.
      >
      > arrayField.Fiel dType shows "MyArray[]", this is correct for me. If I try
      > with
      >
      > arrayField.SetV alue(records.To Array(), instanceOfMyCla ss);
      >
      > to assign the Array I get the error message that the objecttype can't be
      > converted to the destination type.
      > That's right, the object type is "object[]" and the destination type is
      > "MyArray[]".
      >
      > How can I do the typecast at runtime?
      > Or is there another way to do this?
      >
      > --
      > Sleepless in Berlin
      > Wernfried[/color]


      Comment

      • Wernfried Schwenkner

        #4
        Re: Reflection with array

        In article <uwq4ITfWEHA.25 20@TK2MSFTNGP12 .phx.gbl>, 100@100.com says...
        [color=blue]
        >
        > arrayField.SetV alue(instanceOf MyClass, records.ToArray ());
        >
        > Try that and tell if the probles is still there
        >[/color]
        Thanks. This was the problem. I certainly got confused with my variabale
        names and missed the order.

        --
        Sleepless in Berlin
        Wernfried

        Comment

        • Wernfried Schwenkner

          #5
          Re: Reflection with array

          In article <uwq4ITfWEHA.25 20@TK2MSFTNGP12 .phx.gbl>, 100@100.com says...
          [color=blue]
          >
          > arrayField.SetV alue(instanceOf MyClass, records.ToArray ());
          >
          > Try that and tell if the probles is still there
          >[/color]
          Thanks. This was the problem. I certainly got confused with my variabale
          names and missed the order.

          --
          Sleepless in Berlin
          Wernfried

          Comment

          Working...