6

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?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
kptlronyttcna
  • 1,461
  • 1
  • 16
  • 22

3 Answers3

5

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);
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • @kptlronyttcna You welcome. Perhaps the compiler is able to infer *some* of the type arguments for you. I'm not sure which, try combinations yourself. – Yuval Itzchakov Dec 02 '15 at 08:11
2

The biggest hint here is that you're having to be explicit for the type parameters in your GraphMLExtensions.SerializeToGraphML() call.

I took a quick look at the source for this, and realized what's up.

You are using this overload:

public static void SerializeToGraphML<TVertex, TEdge, TGraph>(
    this TGraph graph,
         XmlWriter writer)
         where TEdge : IEdge<TVertex>
         where TGraph : IEdgeListGraph<TVertex, TEdge>

Here TEdge and TGraph need to be set to specific types, but there are no arguments that match the type parameters. This means you have to explicitly set them.

Christopher Stevenson
  • 2,843
  • 20
  • 25
2

You need to specify the generic types, so that the correct generic method will be used. Just add them for calling the generic method:

// Your using block, ...
g.SerializeToGraphML<string, Edge<string>, AdjacencyGraph<string, Edge<string>>>(xwriter);
BendEg
  • 20,098
  • 17
  • 57
  • 131