如何获取通用列表中类型的字节大小?

发布于 2024-12-02 09:42:27 字数 614 浏览 3 评论 0原文

我有这个通用列表,我想获取类型的字节大小,例如 T 是 string 或 int 等,我尝试了 getByteSize() 中编写的两种方法,只是为了让您知道我只使用一种方法一次......

但是当我尝试编译时,它给出了一个错误,提示“错误:找不到类型或命名空间名称'typeParameterType'(您是否缺少using指令或程序集引用?)”

public class iList<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);
        }
    }

并且知道我是什么我在这里做错了吗?

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 iList<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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南城追梦 2024-12-09 09:42:27

sizeof 仅适用于值类型。

对于字符串,在填充之前您不会知道实际字节大小。

如果您打算这样做,请序列化列表并然后对其进行测量。虽然不是一种有保证的方法,但它可能比其他方法更好。 从头开始​​。如果没有真正的努力,它不会让你得到你想要的东西,如果有的话。您可以像这样执行快速而肮脏的计数:

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;
}

如果您确实想了解有关大小的更多信息,这里有一个 关于该主题的全面答案

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.

╰つ倒转 2024-12-09 09:42:27

sizeof 仅适用于非托管类型,例如内置类型(intfloatchar 等...)。 对于引用类型,它只是返回指针的大小(对于 32 位系统,通常为 4) 对于引用/托管类型,它根本不起作用(尝试一下看看)。

此外,您没有向其传递类型,而是向其传递 Type 类型的对象。

您可能想尝试使用 Marshal.SizeOf 相反,但是我不确定这会给您带来您想要的结果,首先只会返回编组后类型的大小,而不是 CLR 分配的大小。由此推论,这也只适用于可以编组的类型,而列表则不能。

您到底想做什么?

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?

饮惑 2024-12-09 09:42:27

您可以使用 Marshal.SizeOf(typeof(T)) ,但请注意,它可能会抛出未知大小的类型。请注意 Marshal.SizeoOf(typeof(char)) == 1

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文