Click here to Skip to main content
15,885,365 members
Articles / Hosted Services / Serverless

LinqToSQL: Comprehensive Support for SQLite, Microsoft Access, SQServer2000/2005

Rate me:
Please Sign up or sign in to vote.
4.97/5 (19 votes)
12 Feb 2008LGPL35 min read 176.5K   2.7K   111  
Use LinqToSql to query these popular RDMS products
// Comes from the LINQ samples provided by Microsoft

//Copyright (C) Microsoft Corporation.  All rights reserved.

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;

namespace LinqToSql {
    public class ObjectDumper {

        public static void Write(object element) {
            Write(element, 0);
        }

        public static void Write(object element, int depth) {
            ObjectDumper dumper = new ObjectDumper(depth);
            dumper.WriteObject(null, element);
        }

        int pos;
        int level;
        int depth;

        private ObjectDumper(int depth) {
            this.depth = depth;
        }

        private void Write(string s) {
            if (s != null) {
                Debug.Write(s);
                pos += s.Length;
            }
        }

        private void WriteIndent() {
            for (int i = 0; i < level; i++) {
                Debug.Write("  ");
            }
        }

        private void WriteLine() {
            Debug.WriteLine(string.Empty);
            pos = 0;
        }

        private void WriteTab() {
            Write("  ");
            while (pos % 8 != 0) Write(" ");
        }

        private void WriteObject(string prefix, object element) {
            if (element == null || element is ValueType || element is string) {
                WriteIndent();
                Write(prefix);
                WriteValue(element);
                WriteLine();
            }
            else {
                IEnumerable enumerableElement = element as IEnumerable;
                if (enumerableElement != null) {
                    foreach (object item in enumerableElement) {
                        if (item is IEnumerable && !(item is string)) {
                            WriteIndent();
                            Write(prefix);
                            Write("...");
                            WriteLine();
                            if (level < depth) {
                                level++;
                                WriteObject(prefix, item);
                                level--;
                            }
                        }
                        else {
                            WriteObject(prefix, item);
                        }
                    }
                }
                else {
                    MemberInfo[] members = element.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance);
                    WriteIndent();
                    Write(prefix);
                    bool propWritten = false;
                    foreach (MemberInfo m in members) {
                        FieldInfo f = m as FieldInfo;
                        PropertyInfo p = m as PropertyInfo;
                        if (f != null || p != null) {
                            if (propWritten) {
                                WriteTab();
                            }
                            else {
                                propWritten = true;
                            }
                            Write(m.Name);
                            Write("=");
                            Type t = f != null ? f.FieldType : p.PropertyType;
                            if (t.IsValueType || t == typeof(string)) {
                                WriteValue(f != null ? f.GetValue(element) : p.GetValue(element, null));
                            }
                            else {
                                if (typeof(IEnumerable).IsAssignableFrom(t)) {
                                    Write("...");
                                }
                                else {
                                    Write("{ }");
                                }
                            }
                        }
                    }
                    if (propWritten) WriteLine();
                    if (level < depth) {
                        foreach (MemberInfo m in members) {
                            FieldInfo f = m as FieldInfo;
                            PropertyInfo p = m as PropertyInfo;
                            if (f != null || p != null) {
                                Type t = f != null ? f.FieldType : p.PropertyType;
                                if (!(t.IsValueType || t == typeof(string))) {
                                    object value = f != null ? f.GetValue(element) : p.GetValue(element, null);
                                    if (value != null) {
                                        level++;
                                        WriteObject(m.Name + ": ", value);
                                        level--;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private void WriteValue(object o) {
            if (o == null) {
                Write("null");
            }
            else if (o is DateTime) {
                Write(((DateTime)o).ToShortDateString());
            }
            else if (o is ValueType || o is string) {
                Write(o.ToString());
            }
            else if (o is IEnumerable) {
                Write("...");
            }
            else {
                Write("{ }");
            }
        }
    }
}

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, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Technical Lead Olivine Technology
Kenya Kenya
Technical Lead, Olivine Technology - Nairobi, Kenya.

"The bane of productivity: confusing the rituals of work (sitting at your desk by 8:00am, wearing a clean and well pressed business costume etc.) with actual work that produces results."

Watch me!

Comments and Discussions