gpt4 book ai didi

c# - 扩展方法不起作用(快速图形序列化)

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

Error: The type arguments for method GraphMLExtensions.SerializeToGraphML<TVertex, TEdge, TGraph>(TGraph,
XmlWriter)
cannot be inferred from the usage.

using System.Xml;
using QuickGraph;
using QuickGraph.Serialization;

var g = new AdjacencyGraph<string, Edge<string>>();

.... add some vertices and edges ....

using (var xwriter = XmlWriter.Create("somefile.xml"))
g.SerializeToGraphML(xwriter);

代码是从 QuickGraph 的文档中复制的。然而,当我明确地写它时它起作用了:

using (var xwriter = XmlWriter.Create("somefile.xml"))
GraphMLExtensions.SerializeToGraphML<string, Edge<string>, AdjacencyGraph<string, Edge<string>>>(g, xwriter);

编辑:我看到了一些相关的问题,但它们对我来说太高级了。我只是担心使用它。是我做错了什么还是文档出了问题?

最佳答案

Am I doing something wrong or it's the documentation?

问题不在于扩展方法。问题在于,当您使用完整的静态方法路径时,您显式地提供了通用类型参数,而使用扩展方法时您根本没有提供任何参数。

实际错误与编译器无法为您推断所有泛型类型参数这一事实有关,需要您显式传递它们的帮助。

这会起作用:

using (var xwriter = XmlWriter.Create("somefile.xml"))
{
g.SerializeToGraphML<string, Edge<string>,
AdjacencyGraph<string, Edge<string>>>(xwriter);
}

关于c# - 扩展方法不起作用(快速图形序列化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037315/

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