internal static ITraversal GetTraversalFromTokens(IList <Token> tokens, GraphTraversalSource g, IDictionary <string, object> contextParameterValues, string traversalText) { object instance; Type instanceType; if (tokens[0].Name == "g") { instance = g; instanceType = g.GetType(); } else if (tokens[0].Name == "__") { instance = null; instanceType = typeof(__); } else { throw BuildException(traversalText); } for (var i = 1; i < tokens.Count; i++) { var token = tokens[i]; token.SetContextParameterValues(contextParameterValues); var name = GetCsharpName(token.Name); var methods = instanceType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) .Where(m => m.Name == name).ToList(); var method = GetClosestMethod(methods, token.Parameters); if (method == null) { throw new InvalidOperationException($"Traversal method '{tokens[i].Name}' not found for testing"); } var parameterValues = BuildParameters(method, token, out var genericParameters); method = BuildGenericMethod(method, genericParameters, parameterValues); instance = method.Invoke(instance, parameterValues); instanceType = instance.GetType(); } return((ITraversal)instance); }