CSharp examples for System:Object
Compute the size of an object.
/*/* w w w . j a v a 2 s.c om*/ * Copyright: Thomas McGlynn 1997-2007. * * The CSharpFITS package is a C# port of Tom McGlynn's * nom.tam.fits Java package, initially ported by Samuel Carliles * * Copyright: 2007 Virtual Observatory - India. * * Use is subject to license terms */ using System.Collections; using System; public class Main{ /// <summary>Compute the size of an object. Note that this only handles /// arrays or scalars of the primitive objects and Strings. It /// returns 0 for any object array element it does not understand.</summary> /// <param name="o">The object whose size is desired.</param> public static int ComputeSize(Object o) { int result = 0; if (o != null) { Type t = o.GetType(); if (t.IsArray) { int size = 0; for (IEnumerator i = ((Array)o).GetEnumerator(); i.MoveNext(); ) { size += ComputeSize(i.Current); } //return size; result = size; } else if (t == typeof(String)) { //return ((String)o).Length * (int)sizes[typeof(char)]; result = ((String)o).Length * (int)sizes[typeof(char)]; } else if (t == typeof(Troolean)) { result = 1; } else if (t.IsPrimitive) { //return (int)sizes[t]; result = (int)sizes[t]; } else { //return 0; result = 0; } } return result; } }