How to determine size of a value-type using Mono.Cecil


I'm looking a way to determine size in bytes of a value type (TypeReference) in Mono.Cecil.

I need it to optimize Nullable equality compare code generation.

  • If value type size > 4, compare HasValue first, then compare values.
  • If value type size <= 4, compare GetValueOrDefault first, then compare HasValue.

But to do so, I need to get somehow sizeof(T).


It's not 100% perfect (since since will vary based on 32/64 bits arch) but Gendarme has the code to do this task. Look at the SizeOf method inside:

By : poupou

This video can help you solving your question :)
By: admin