I just ran into the problem of "custom conversions to or from an interface that are not allowed" in C #. What I was trying to do was create a generic Graph class that could be repeated in several ways, depending on the supported interface. So:
public class Graph<T> : IBreadthFirstSearchTree<T>, IDepthFirstSearchTree<T> { // unnecessary details public static explicit operator IBreadthFirstSearchTree<T>(Graph<T> g) { g.enumerator = new GraphEnumerator<T>(g, SortStrategy.BreadthFirst); return g as IBreadthFirstSearchTree<T>; } public static explicit operator IDepthFirstSearchTree<T>(Graph<T> g) { g.enumerator = new GraphEnumerator<T>(g, SortStrategy.DepthFirst); return g as IDepthFirstSearchTree<T>; } }
was intended for this use:
foreach (GraphNode<T> gn in myGraph as IDepthFirstSearchTree) {
Does anyone know how I can achieve the same syntax results within language restrictions?
c #
Ben Collins Nov 21 '08 at 14:49 2008-11-21 14:49
source share