C# · 12月 25, 2021

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

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>>>(xwriter);}