I want to write method which calculate value types size. But i can't give value types (int, double, float) as method parameter.
/*
*When i call this method with SizeOf<int>() and
*then it returns 4 bytes as result.
*/
public static int SizeOf<T>() where T : struct
{
return Marshal.SizeOf(default(T));
}
/*
*When i call this method with TypeOf<int>() and
*then it returns System.Int32 as result.
*/
public static System.Type TypeOf<T>()
{
return typeof(T);
}
I don't want it that way.I want to write this method as below.
/*
*When i call this method with GetSize(int) and
*then it returns error like "Invalid expression term 'int'".
*/
public static int GetSize(System.Type type)
{
return Marshal.SizeOf(type);
}
So how can i pass value types (int, double, float, char ..) to method parameters to calculate it's size as generic.