Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine C# Code size [closed]

Tags:

c#

.net

I'd like to check how big is the compiled code of some classes (in bytes). I'd like to optimize them for size, but I need to know where to start.

like image 374
Adam Kłobukowski Avatar asked Nov 21 '11 13:11

Adam Kłobukowski


People also ask

How do you do exponents in C?

Basically in C exponent value is calculated using the pow() function. pow() is function to get the power of a number, but we have to use #include<math. h> in c/c++ to use that pow() function.

How do you write square root in C?

The sqrt() function is defined in math. h header file. To find the square root of int , float or long double data types, you can explicitly convert the type to double using cast operator. int x = 0; double result; result = sqrt(double(x));


1 Answers

One method will be to get the size of MSIL with Reflection. You will need to loop through all methods, Property setters and getters, and constructors to determine the size of the MSIL. Also, there will be differences in size based on Debug versus Release builds.

using System.Reflection;

int totalSize = 0;

foreach(var mi in typeof(Example).GetMethods(BindingFlags.Public | BindingFlags.NonPublic |BindingFlags.Static | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty))
{
  MethodInfo mi = typeof(Example).GetMethod("MethodBodyExample");
  MethodBody mb = mi.GetMethodBody();
  totalSize += mb.GetILAsByteArray().Length;
}
like image 200
Simon Avatar answered Oct 04 '22 15:10

Simon