Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

You can try reflection for the same.

C#
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;
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900