GroupCollection.First 在 netcoreapp 中构建,但不在 netstandard 中构建

daw*_*daw 2 c# .net-core

netcoreapp我在构建代码时遇到错误,netstandard无法解决。

以下代码编译为netcoreapp2.2

using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace TestNamespace
{
    public class TestClass
    {
        public static Group Example(string str, string pattern) =>
            Regex.Match(str, pattern).Groups.First();
    }
}
Run Code Online (Sandbox Code Playgroud)

但如果我将其更改为netstandard2.0.First无法编译:

  Class1.cs(10, 46): [CS1061] 'GroupCollection' does not contain a definition for 'First' and no accessible extension method 'First' accepting a first argument of type 'GroupCollection' could be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)

但是,如果我在 Jetbrains Rider 中使用“转到代码”,则反汇编会GroupCollection解析为System.Text.RegularExpressions, Version=4.2.1.0whichimplements IList。我已手动添加此程序集,System.Linq但错误仍然存​​在。

知道发生了什么吗?有解决办法吗?

mjw*_*lls 5

GroupCollection在更高版本的 .NET Core 中实现了 IList<Group>. IList<Group>足以让 LINQ 扩展方法(如First)发挥作用。

GroupCollection.NET Framework(或早期版本的 .NET Core)中不实现该接口(它仅实现旧的(非通用)接口)。First因此,如果不投射它就无法使用。

如果您确实决定这样做Cast,那么相同的代码将适用于所有内容(.NET Core / Standard / Framework)。