I'm trying to do something along the following lines without having to manually type the type identifiers:
Code:
Console.WriteLine(typeof(ushort) + ", bits: " + sizeof(ushort) * 8);
Console.WriteLine(typeof(int) + ", bits: " + sizeof(int) * 8);
Console.WriteLine(typeof(uint) + ", bits: " + sizeof(uint) * 8);
Console.WriteLine(typeof(long) + ", bits: " + sizeof(long) * 8);
Console.WriteLine(typeof(ulong) + ", bits: " + sizeof(ulong) * 8);
Console.WriteLine(typeof(float) + ", bits: " + sizeof(float) * 8);
Console.WriteLine(typeof(double) + ", bits: " + sizeof(double) * 8);
Console.WriteLine(typeof(decimal) + ", bits: " + sizeof(decimal) * 8);
Console.WriteLine(typeof(IntPtr) + ", bits: " + sizeof(IntPtr) * 8);
Console.WriteLine(typeof(UIntPtr) + ", bits: " + sizeof(UIntPtr) * 8);
Console.WriteLine(typeof(DateTime) + ", bits: " + sizeof(DateTime) * 8);
Console.WriteLine(typeof(ConsoleColor) + ", bits: " + sizeof(ConsoleColor) * 8);
Console.WriteLine(typeof(TimeSpan) + ", bits: " + sizeof(TimeSpan) * 8);
true, instead of relying on the typeof operator I can use the GetType() method, but I'm still left with no known way of calling the sizeof operator.
I know that I could, for example, create an instance of the System.Reflection.Assembly class (by assigned a reference to an instance of a derived type of course) and call the GetTypes() method on it, select desired types (ie: value types) then loop through the System.Type[] array object and print relevant info. However, I'm still unsure there is a proper substitute for the sizeof operator, which in addition to working only in an unsafe context, it works only with some value types (ie: enums, built-in value types, user-defined structs as long as they do not have fields of managed types.)
A nice alternative would be to define a generic method that infers the type parameter based on the method argument (ie: Print<T>(T t){..}) which can then be called with only method argument and no type argument, but the sizeof operator sort of messes things up because it doesn't work with all types. adding a generic constraint to the Print<T> method seems like it should work but even "where T : struct" will cause problems because the sizeof operator will not work with all value types.
Who knows, maybe System.Reflection offers a runtime alternative to the sizeof operator.
Any thoughts?