简体   繁体   English

点网核心不一致

[英]Dot Net Core being Inconsistent

I have a problem in my application that when GetCustomAttributes is called on the assembly Sigil v4.7, it throws an exception.我的应用程序中有一个问题,当在程序集 Sigil v4.7 上调用GetCustomAttributes时,它会引发异常。

But this only occurs in a specific situation.但这仅发生在特定情况下。 If Sigil v4.7 is added via Jil v2.15.4 then it will happen.如果通过 Jil v2.15.4 添加 Sigil v4.7,那么它就会发生。

If you add Sigil v4.7 directly it does not repro.如果您直接添加 Sigil v4.7,它不会复制。 Similarly if you add the latest version of Jil (v2.17.0), which still references Sigil v4.7, it does not repro.同样,如果您添加仍然引用 Sigil v4.7 的最新版本的 Jil (v2.17.0),它不会重现。

I am stumped as to why, given the same dll, I would get different results.我不知道为什么,给定相同的 dll,我会得到不同的结果。

Here is my test code:这是我的测试代码:

using System.Diagnostics;
using System.Linq;
using System.Reflection;

namespace WindowsAssemblyTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var sigilAssembly = AppDomain.CurrentDomain.GetAssemblies()
                                         .FirstOrDefault(x => x.FullName.Contains("Sigil"));

            try
            {
                var attrs = sigilAssembly?.GetCustomAttributes(typeof(AssemblyProductAttribute), false)
                                          .OfType<AssemblyProductAttribute>();

                Debug.WriteLine(">>>>>>  Success: " + sigilAssembly?.GetName());
            }
            catch (System.IO.FileNotFoundException exception)
            {
                Debug.WriteLine(">>>>>>  Failed: " + sigilAssembly?.GetName() + " with " + exception.Message);
            }

            // This is here so the compiler does not remove the reference to Sigil.
            var createReferenceToSigil = Sigil.OptimizationOptions.All;    
        }
    }
}

To recreate this issue, create a .net core console app and add the above code to the program.cs .要重现此问题,请创建 .net 核心控制台应用程序并将上述代码添加到program.cs Then add a NuGet reference to Jil v2.15.4 and run.然后将 NuGet 引用添加到 Jil v2.15.4 并运行。 The Debug window will show:调试 window 将显示:

>>>>>> Failed: Sigil, Version=4.7.0.0, Culture=neutral, PublicKeyToken=2d06c3494341c8ab with Could not load file or assembly 'System.Runtime.InteropServices.PInvoke, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. >>>>>> 失败:Sigil,Version=4.7.0.0,Culture=neutral,PublicKeyToken=2d06c3494341c8ab 无法加载文件或程序集'System.Runtime.InteropServices.PInvoke,Version=4.0.0.0,Culture=neutral,PublicKeyToken =b03f5f7f11d50a3a'。 The system cannot find the file specified.该系统找不到指定的文件。

If you then upgrade to Jil v2.17.0 you will see this:如果您随后升级到 Jil v2.17.0,您将看到:

>>>>>> Success: Sigil, Version=4.7.0.0, Culture=neutral, PublicKeyToken=2d06c3494341c8ab >>>>>> 成功:印记,版本=4.7.0.0,文化=中性,PublicKeyToken=2d06c3494341c8ab

Why would the same version of the dll Sigil fail for one version of Jil and not for another?为什么相同版本的 dll Sigil 会在一个版本的 Jil 上失败,而在另一个版本上失败?

I don't think the problem has anything to do with Sigil.我不认为这个问题与印记有任何关系。 As you mentioned in your comment, it finds Sigil just fine, but fails on GetCustomAttributes .正如您在评论中提到的,它发现 Sigil 很好,但在GetCustomAttributes上失败了。

And that's the problem: GetCustomAttributes relies on System.Runtime.InteropServices.PInvoke .这就是问题所在: GetCustomAttributes依赖于System.Runtime.InteropServices.PInvoke

So why can it find PInvoke when you're using Jil v2.17.0 but not v2.15.4?那么,当您使用 Jil v2.17.0 而不是 v2.15.4 时,为什么它可以找到PInvoke

Look at the dependencies of both Jil v2.17.0 and Jil v2.15.4 .查看Jil v2.17.0Jil v2.15.4的依赖关系。 The difference is that 2.17.0 supports .NETStandard 2.0, but v2.15.4 only supports .NETStandard 1.6.不同的是2.17.0支持.NETStandard 2.0,而v2.15.4只支持.NETStandard 1.6。

It's hard to find something that nails this down as exactly the problem, but if you search for "System.Runtime.InteropServices.PInvoke" "GetCustomAttributes" you will find several results that explain that System.Runtime.InteropServices.PInvoke was not available in .NET Core 1.0.很难找到确切的问题,但如果您搜索“System.Runtime.InteropServices.PInvoke”“GetCustomAttributes” ,您会发现几个结果解释System.Runtime.InteropServices.PInvoke在.NET 核心 1.0。 (it was in the release candidate, but removed before release ) (它在候选发布版本中,但在发布之前被删除

So I believe what's happening is that if you use the newer package, it's running under .NETStandard 2.0 and PInvoke is available.所以我相信正在发生的事情是,如果您使用更新的 package,它在 .NETStandard 2.0 下运行并且PInvoke可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM