如何在asp.net中获取对象的大小 [英] How to get size of an object in asp.net

查看:64
本文介绍了如何在asp.net中获取对象的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友.

我必须获取对象的大小.
例如
我的页面中有一个数据集对象,我在其中动态填充数据.现在,我想获取此数据集对象的大小,以了解此对象分配了多少内存.

谢谢
Imrankhan

Hello friends.

I have to get the size of an object.
for example
I have one dataset object in my page where I am filling data dynamically. Now I want to get the size of this dataset object to know how much memory is allocated by this object.

thanks
Imrankhan

推荐答案

您可以尝试同样的反射.

You can try reflection for the same.

public static int GetSizeOfObject(object obj)
{
 object Value = null;
 int size = 0;
 Type type = obj.GetType();
 PropertyInfo[] info = type.GetProperties();
 foreach(PropertyInfo property in info)
 {
  Value = property.GetValue(obj,null);
  unsafe
  {
   size += sizeof(Value);
  }
 }
 return size;


嗨.感谢您的代码.但是在放置不安全线的地方出现错误.它说不安全代码只有在使用/unsafe进行编译时才可能出现.

谢谢
Hi. thanks for code. but I am getting error where unsafe line is put. It says Unsafe code may only appear if compiling with /unsafe.

Thanks


这篇关于如何在asp.net中获取对象的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆