Search code examples
c#.netquickgraph

Extension method doesn't work (Quick Graph Serialization)


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);

The code is copied from QuickGraph's documentation. However when I write it explicitly it works:

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

Edit: I saw some related questions, but they are too advanced for me. I'm just concerned about using it. Am I doing something wrong or it's the documentation?


Solution

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

    The problem isn't with the extension method. The problems lays in the fact that when you use the full static method path, you're suppling the generic type arguments explicitly, while using the extension method you're not supplying any at all.

    The actual error is related to the fact that compiler can't infer all the generic type arguments for you, and needs your help by explicitly passing them.

    This will work:

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