Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 344
kptlronyttcna Avatar asked Dec 02 '15 07:12

kptlronyttcna


2 Answers

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);
}
like image 128
Yuval Itzchakov Avatar answered Sep 20 '22 20:09

Yuval Itzchakov


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.

like image 29
Christopher Stevenson Avatar answered Sep 20 '22 20:09

Christopher Stevenson