Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MatchCollection类型集合中项的基础类型是什么?_C#_.net - Fatal编程技术网

C# MatchCollection类型集合中项的基础类型是什么?

C# MatchCollection类型集合中项的基础类型是什么?,c#,.net,C#,.net,我假设它的类型是Match,但这不正确,因为我得到了错误 无法隐式转换类型“System.Collections.IEnumerator” 打字 'System.Collections.Generic.IEnumerator 在线 IEnumerator<Match> friendsULsEnumerator = friendsULs.GetEnumerator(); 将鼠标悬停在var上,查看Visual Studio是否告诉了我具体的类型,但它只显示了IEnumerable:(

我假设它的类型是
Match
,但这不正确,因为我得到了错误

无法隐式转换类型“System.Collections.IEnumerator” 打字 'System.Collections.Generic.IEnumerator

在线

IEnumerator<Match> friendsULsEnumerator = friendsULs.GetEnumerator();
将鼠标悬停在
var
上,查看Visual Studio是否告诉了我具体的类型,但它只显示了
IEnumerable
:(

为了将我的问题扩大到更大的问题,我试图从
字符串中获得唐纳德·特朗普、希拉里·克林顿等的价值观

<h2>Friends</h2><ul><li>Donald Trump</li><li>Hillary Clinton</li>...</ul>
朋友们
  • 唐纳德·特朗普
  • 希拉里·克林顿
  • .
在一个较大的字符串中只出现一次,所以我得到的是

                MatchCollection friendsULs = DataReader._fregx.Matches(sr.ReadToEnd());
                if ( friendsULs.Count != 1 ) throw new Exception(String.Format("Couldn't find exactly one piece of HTML matching {0}", 
                                                                                DataReader._fregx.ToString()));
                IEnumerator<Match> friendsULsEnumerator = friendsULs.GetEnumerator();
                if ( friendsULsEnumerator.MoveNext() )  { } // because MoveNext() returns a bool, this useless block is necessary
                MatchCollection friendsLIs = DataReader._lregx.Matches(friendsULsEnumerator.Current.ToString()); 
MatchCollection-friendsULs=DataReader.\u fregx.Matches(sr.ReadToEnd());
如果(friendsULs.Count!=1)抛出新的异常(String.Format(“找不到与{0}匹配的HTML片段”),
DataReader.\u fregx.ToString());
IEnumerator friendsULsEnumerator=friendsULs.GetEnumerator();
如果(friendsULsEnumerator.MoveNext()){}//因为MoveNext()返回bool,那么这个无用的块是必需的
MatchCollection-friendsLIs=DataReader.\u lregx.Matches(friendsULsEnumerator.Current.ToString());

但是,也许你可以建议一种更简洁、更优雅的方法来完成这项工作。

匹配集合的底层类型实际上是一个
匹配
GetEnumerator()
返回一个简单的
IEnumerator
(即版本预定日期泛型,其中所有元素都是一个对象,您需要自己铸造它们。)

通常,您将以如下方式处理循环中的所有项:

for (int i = 0; i < friendULs.Count; i++)
{
    Match thisMatch = friendULs[i];
    ...
}
for(int i=0;i

您可以使用
friendsULs.OfType()

MatchCollection
转换为
IEnumerable
,您可以使用以下方法轻松迭代匹配:

foreach(Match match in friendsULs)
   // do whatever with match here

这是迭代枚举的NET 1.0模式,因为那时没有泛型。

您调用的方法,
GetEnumerator
是由非泛型接口定义的方法。它返回一个非泛型
IEnumerator
,与
IEnumerator不同
。为了回答您的问题:
MatchCollection
确实是
Match
对象的集合,但由于它没有实现通用的
IEnumerable
接口,您需要“强制转换”手动。OfType
方法的作用就是:将非泛型
IEnumerable
集合转换为指定类型的泛型集合

也就是说,
GetEnumerator
是“canonical C#”中很少用到的一种方法。从您的代码示例来看,您似乎只需要获得集合中的第一个
匹配项
。使用LINQ,这一点更简单、更优雅,例如使用
Single
操作符:

Match match = friendsULs.OfType<Match>().Single();
Match Match=friendsULs.OfType().Single();

用于matchcollectoin(底部有示例)。您从哪里获得此html以及它是xhtml?请检查此问题()答案显而易见——HTML敏捷性Pack@vittore该页面上的示例并没有回答我的问题,也许这一个可以说明@Steve在我的情况下,我正在阅读的文件的HTML格式是一致的,对于我来说,使用正则表达式搜索没有任何潜在的问题是很好的,但我不确定它是否是一致的ks我的问题是:
GetEnumerator()返回的类型
IEnumerable
中的
是什么
调用了一个
MatchCollection
对象?它只是一个
IEnumerable
而不是
IEnumerable
。我已经更新了我的答案来澄清。我不想重复它们。我只想要第一个也是唯一一个。@然后使用
Regex.Match
而不是
Regex.Matches
我非常困惑n、 为什么不直接执行
friendULs[0]
?它具有正确的
匹配类型。
Match match = friendsULs.OfType<Match>().Single();