gpt4 book ai didi

c# - 带有 Windows API 方法签名、结构、常量 : will they all be included in the final . exe 的大型 C# 源文件?

转载 作者:太空狗 更新时间:2023-10-29 18:30:10 25 4
gpt4 key购买 nike

我想将我在程序中使用的 Windows API 函数的所有签名放在一个类中,例如 WinAPI,并放在我将包含在我的项目中的文件 WinAPI.cs 中。该类将是internal static,方法是public static extern。 (类似于 .NET Framework 源代码中巨大的 NativeMethods.cs)。

假设 WinAPI.cs 有一百个 native 方法签名,例如:

[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  1. 如果我只打算在一个项目中使用其中的几个(甚至一两个),如果我在我的项目中包含 WinAPI.cs 文件,那么最终的 .exe 是否会充满不必要的我没有使用的方法声明(其他 99 个左右)?

  2. 原生结构声明呢?如果我没有在当前项目中使用它们,它们是否仍会包含在最终的 .exe 中?

        [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
    public int X;
    public int Y;
    }
  3. 关于常量声明或只读成员,如:

    public const int WH_JOURNALPLAYBACK = 1;
    public static readonly int WM_GETTEXT = 0x0D;

最佳答案

不要为这里的小事担心。这些只是声明,在最终组装中没有针对它们的 IL。它们只是占用元数据中的一些空间。它们甚至没有从程序集中加载的可能性非常高,尤其是当您将声明放在一起时。元数据仅在使用时加载到 RAM 中,一次 4096 字节。

顺便说一句,您明确不想对这些常量声明使用readonly。现在您确实让它们占用空间,即使它们未被使用。公共(public) const 是个好主意的少数情况之一。这些是明显的常量,它们的值永远不会改变。像 Math.PI。

关于c# - 带有 Windows API 方法签名、结构、常量 : will they all be included in the final . exe 的大型 C# 源文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4314865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com