I want to put all the signatures of Windows API functions I'm using in programs in one class, such as WinAPI, and in a file WinAPI.cs I will include in my projects. The class will be internal static and the methods public static extern. (Something like the huge NativeMethods.cs from the .NET Framework source code).
Let's say WinAPI.cs has one hundred native methods signatures like:
[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
If I'm only going to use a few of them in a project (even one or two), if I include the WinAPI.cs file in my project, will the final .exe be filled with the unnecessary method declarations I'm not using (the other 99 or so)?
What about native structure declarations, If I'm not using them in the current project, will they still be included in the final .exe?
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
What about constants declarations or readonly members like:
public const int WH_JOURNALPLAYBACK = 1;
public static readonly int WM_GETTEXT = 0x0D;
Don't sweat the small stuff here. These are just declarations, there is no IL for them in the final assembly. They just take up some space in the metadata. Odds are very high that they don't even get loaded from the assembly, especially when you keep the declarations together. Metadata is only ever loaded into RAM when it is used, 4096 bytes at a time.
Btw, you explicitly don't want to use readonly for these constant declarations. Now you do get them to take space, even when they are not used. One of the few cases where a public const is a good idea. These are manifest constants, their value never changes. Like Math.PI.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With