Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

LINQ To Google Image and Google Groups

Rate me:
Please Sign up or sign in to vote.
5.00/5 (17 votes)
8 May 200710 min read 90.9K   400   48  
A LINQ Implementation for Google Images/Groups Search
using System;
using System.Collections.Generic;
using System.Text;

using System.Linq;
using System.Linq.Expressions;

namespace MChen.Linq.GoogleSearch.Common
{
    /// <summary>
    /// Diagnostic class for debug purposes
    /// </summary>
    internal class Diagnostic
    {
        public static void DebugExpressionTree(Expression expression)
        {
            InternalDebugExpressionTree(expression, "Top", 0);
        }

        private static string Indent(int num)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < num; ++i) sb.Append("   ");
            return sb.ToString();
        }

        private static void InternalDebugExpressionTree(Expression expression, string type, int depth)
        {
            Console.Write("{0}{1} is {2}:  ", Indent(depth), type, expression.GetType().Name);
            switch (expression.NodeType)
            {
                case ExpressionType.ArrayLength:
                case ExpressionType.Convert:
                case ExpressionType.ConvertChecked:
                case ExpressionType.Negate:
                case ExpressionType.NegateChecked:
                case ExpressionType.Not:
                case ExpressionType.TypeAs:
                case ExpressionType.Quote:
                    UnaryExpression ue = expression as UnaryExpression;
                    Console.WriteLine("{0}", ue.NodeType);
                    InternalDebugExpressionTree(ue.Operand, "Operand", depth + 1);
                    break;
                case ExpressionType.Call:
                    MethodCallExpression mce = expression as MethodCallExpression;
                    Console.WriteLine("{0}", mce.Method.Name);
                    if (mce.Object != null)
                        InternalDebugExpressionTree(mce.Object, "Object", depth + 1);
                    foreach (Expression ex in mce.Arguments)
                        InternalDebugExpressionTree(ex, "Param", depth + 1);
                    break;
                case ExpressionType.Constant:
                    ConstantExpression ce = expression as ConstantExpression;
                    Console.WriteLine("{0} = {1}", ce.Value.GetType().Name, ce.Value );
                    break;
                case ExpressionType.Lambda:
                    LambdaExpression lex = expression as LambdaExpression;
                    Console.WriteLine("{0}", expression.NodeType);
                    InternalDebugExpressionTree(lex.Body, "Body", depth + 1);
                    foreach (Expression exp in lex.Parameters)
                    {
                        InternalDebugExpressionTree(exp, "Param", depth + 1);
                    }
                    break;
                case ExpressionType.Parameter:
                    ParameterExpression pex = expression as ParameterExpression;
                    Console.WriteLine("Parameter Name = {0}", pex.Name);
                    break;
                case ExpressionType.MemberAccess:
                    MemberExpression mex = expression as MemberExpression;
                    Console.WriteLine("{0}", mex.Member.Name);
                    break;
                //arithmetic
                case ExpressionType.Add:
                case ExpressionType.AddChecked:
                case ExpressionType.Divide:
                case ExpressionType.Modulo:
                case ExpressionType.Multiply:
                case ExpressionType.MultiplyChecked:
                case ExpressionType.Subtract:
                case ExpressionType.SubtractChecked:
                //bitwise
                case ExpressionType.And:
                case ExpressionType.Or:
                case ExpressionType.ExclusiveOr:
                //shift
                case ExpressionType.LeftShift:
                case ExpressionType.RightShift:
                //conditional boolean
                case ExpressionType.AndAlso:
                case ExpressionType.OrElse:
                //comparison
                case ExpressionType.Equal:
                case ExpressionType.NotEqual:
                case ExpressionType.GreaterThan:
                case ExpressionType.GreaterThanOrEqual:
                case ExpressionType.LessThan:
                case ExpressionType.LessThanOrEqual:
                //coalesce
                case ExpressionType.Coalesce:
                //array index
                case ExpressionType.ArrayIndex:
                    BinaryExpression bex = expression as BinaryExpression;
                    Console.WriteLine("{0}", bex.NodeType);
                    InternalDebugExpressionTree(bex.Left, "Left", depth + 1);
                    InternalDebugExpressionTree(bex.Right, "Right", depth + 1);
                    break;

                case ExpressionType.Conditional:
                case ExpressionType.Funclet:
                case ExpressionType.Invoke:
                case ExpressionType.ListInit:
                case ExpressionType.MemberInit:
                case ExpressionType.New:
                case ExpressionType.NewArrayBounds:
                case ExpressionType.NewArrayInit:
                case ExpressionType.TypeIs:
                default:
                    Console.WriteLine("{0}", expression.NodeType);
                    break;
            }
            if (depth == 0)
                Console.WriteLine("==============================================");
            return;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States

Comments and Discussions