how to get byte size of type in generic list?
I have this generic list and I want to get the byte size of the type like if T is string or int etc., I tried both ways as written in getByteSize(), and just to let you know I am using only one way at a time ...
but when I try to compile, it gives an error saying "Error: The type or namespace name 'typeParameterType' could not be found (are you missing a using directive or an assembly reference?)"
public class iLis开发者_运维知识库t<T> : List<T>
{
public int getByteSize ()
{
// way 1
Type typeParameterType = typeof(T);
return sizeof(typeParameterType);
// way 2
Type typeParameterType = this.GetType().GetGenericArguments()[0];
return sizeof(typeParameterType);
}
}
And idea what I am doing wrong here?
sizeof
is only going to work on value types.
For a string, you won't know the actual byte size until you populate it.
If you are set on doing this, serialize the list and measure it then. While not a guaranteed way, it is probably better than the alternative. Scratch that. It won't get you what you want without some real effort, if at all. You could perform a quick and dirty count like so:
public int getListSize()
{
Type type = typeof(T);
if (type.IsEnum)
{
return this.Sum(item => Marshal.SizeOf(Enum.GetUnderlyingType(type)));
}
if (type.IsValueType)
{
return this.Sum(item => Marshal.SizeOf(item));
}
if (type == typeof(string))
{
return this.Sum(item => Encoding.Default.GetByteCount(item.ToString()));
}
return 32 * this.Count;
}
If you really want to know more about size, here is a comprehensive answer on the topic.
sizeof
only works for unmanaged types, such as built in types (int
, float
, char
etc...). For reference types it simply returns the size of a pointer (normally 4 for 32 bit systems) It won't work at all for reference / managed types (try it and see).
Also you aren't passing it a type, you are passing it an object of type Type
.
You might want to try using Marshal.SizeOf
instead, however I'm not sure this will give you what you want, to start with this will only return the size of the type after it has been marshalled, not the size allocated by the CLR. By corollary this will also only work with types that can be marshalled, of which lists cannot.
What exactly is it that you are trying to do?
You can use Marshal.SizeOf(typeof(T))
but be aware that it can throw for types with unknown size. Be aware that Marshal.SizeoOf(typeof(char)) == 1
.
精彩评论