array question

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

    #1

    array question

    I'm having one or more classes that contains only declarations of primitives and primitives initialized arrays
    i.e:
    class PrimitivesStruc ture
    {
    public int32 integer1;
    public char[] array1 = new char[20];
    public int[] array2 = new int[10];
    ...
    }
    How can I found, dianmically, from another class, the length in bytes of declared primitives arrays?
    For primitive fields it's simple:

    PrimitivesStruc ture myObject = new PrimitivesStruc ture();
    foreach (System.Reflect ion.FieldInfo fi in myObject.GetTyp e().GetFields() )
    {
    if (!fi.FieldType. IsArray)
    Console.WriteLi ne(System.Runti me.InteropServi ces.Marshal.Siz eOf(Activator.. CreateInstance( fi.FieldType))) ;
    }

    --
    Horatiu Ripa

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: array question

    Horatiu,

    Technically, what you are doing isn't correct. When you use the static
    SizeOf method on the Marshal class, it is the size of the marshaled data,
    not of the representation in .NET. Granted, there usually is a one-to-one
    relationship between the number of bytes used to represent the type in .NET
    and in the unmanaged realm, but in general, it should not be assumed that is
    the case.

    What you could do is use the sizeof operator, but that is only allowed
    in unsafe code. If that is not acceptable, then you should create a method
    which will map the types to their sizes.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "Horatiu Ripa" <unreal@busines sco.us> wrote in message
    news:%23NmtLsxj DHA.2772@TK2MSF TNGP12.phx.gbl. ..
    I'm having one or more classes that contains only declarations of primitives
    and primitives initialized arrays
    i.e:
    class PrimitivesStruc ture
    {
    public int32 integer1;
    public char[] array1 = new char[20];
    public int[] array2 = new int[10];
    ...
    }
    How can I found, dianmically, from another class, the length in bytes of
    declared primitives arrays?
    For primitive fields it's simple:

    PrimitivesStruc ture myObject = new PrimitivesStruc ture();
    foreach (System.Reflect ion.FieldInfo fi in myObject.GetTyp e().GetFields() )
    {
    if (!fi.FieldType. IsArray)

    Console.WriteLi ne(System.Runti me.InteropServi ces.Marshal.Siz eOf(Activator.C r
    eateInstance(fi .FieldType)));
    }

    --
    Horatiu Ripa


    Comment

    Working...