Calculate Length of XElement that is not Wasteful?
I have a large XElement property, and want to know the byte size of it for logging purposes. I don't want to just ToString() it because I have concerns about potentially big strings (not) 开发者_运维知识库getting GC'd.
What is a smart/compact way to calculate the XML content of an XElement (.NET 4.0).
Thanks.
In C#, there is no easy way to know an object's size. You can recursively compute it knowing the size of the primitive types and using reflection, but it's not an easy job.
I assume you don't really care about the XElement size, but rather about the serialized XML content (they are quite different sizes naturally). I think to get that you need to serialize it (i.e. call ToString()) - there is no way around it.
精彩评论