Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I want to find the size of a non serializable object?
Please help!!!!
Posted
Comments
BillWoodruff 10-Nov-11 10:06am    
Curious: what are you going to do when you know the total size of this object ?
kulkarni.ajay 10-Nov-11 21:45pm    
Simply I want to write that object size in file for performance :)

I found this on MSDN - using google - that free search engine - accessible by everyone - everywhere:

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
 
Comments
kulkarni.ajay 10-Nov-11 21:46pm    
Thanks John for the quick response and for help. :)
K K Shah 13-Feb-19 3:46am    
It gives error When my datatype is DateTime
Ready code provided by John Simmons is good only thing you can add 2/4 bytes in it for how many times your object is getting refer. (Object reference adds in stack).

Regards
Rushi
 
Share this answer
 
Comments
kulkarni.ajay 10-Nov-11 21:46pm    
Thanks Rushi.

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