You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用LibraryImportAttribute出现CS0755错误:误判扩展方法致编译失败

将DllImport替换为LibraryImport后触发CS0755编译错误

使用.NET 8 Preview 6 + Visual Studio 2022环境,将库中的DllImport替换为LibraryImport后,触发了CS0755编译错误。实际代码中并无扩展方法,切回DllImport时错误会转移至下一个使用LibraryImport的方法声明,且错误仅出现一次。

自定义导入方法声明

public unsafe static partial class CppCoreFuncs
{
    #region private
    [LibraryImport(DLL, EntryPoint = "Poly2Cofs_multi_buf"), SuppressUnmanagedCodeSecurity]
    private static partial FuncResult Poly2CofsMultiBuf(void* buffer, InputMode mode);
    #endregion

...
}

自动生成的代码

public unsafe static partial class CppCoreFuncs
{
    [System.Runtime.InteropServices.DllImportAttribute("CPP Core.dll", EntryPoint = "Poly2Cofs_multi_buf", ExactSpelling = true)]
    private static extern partial global::RWT.ROCT.Core.Public.FuncResult Poly2CofsMultiBuf(void* buffer, global::RWT.ROCT.Core.Public.InputMode mode);
}

编译错误信息

Error CS0755 Both partial method declarations must be extension methods or neither may be an extension method...
Core\Microsoft.Interop.LibraryImportGenerator\Microsoft.Interop.LibraryImportGenerator\LibraryImports.g.cs

正常工作的GeneratedRegexAttribute生成代码示例

[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Text.RegularExpressions.Generator", "8.0.8.32907")]
internal static partial global::System.Text.RegularExpressions.Regex QuotedOrUnquotedNewLineRx() => global::System.Text.RegularExpressions.Generated.QuotedOrUnquotedNewLineRx_7.Instance;

内容的提问来源于stack exchange,提问作者IamIC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 13:32:32