Click here to Skip to main content
15,886,735 members
Articles / Programming Languages / SQL

SqlLinq: Taking LINQ to SQL in the Other Direction

Rate me:
Please Sign up or sign in to vote.
4.96/5 (50 votes)
12 Nov 2009CPOL13 min read 103.6K   1.1K   145  
Parsing SQL statements to create LINQ Expressions.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Diagnostics;
using System.Linq;
using System.Globalization;

using File.PropertyProvider;

namespace AssemblyFileProperties
{
    class AssemblyInspector : MarshalByRefObject
    {
        private class AssemblyReflectedPropertyReader : ReflectedPropertyReader
        {
            public AssemblyReflectedPropertyReader()
            {
            }

            protected override bool StoreProperty(PropertyInfo p)
            {
                if (p.Name == "ReflectionOnly") 
                    return false;

                return base.StoreProperty(p);
            }
        }

        private IDictionary<string, object> m_properties = new Dictionary<string, object>();

        public AssemblyInspector()
        {
        }

        public static IEnumerable<KeyValuePair<string, Type>> GetFields()
        {
            ReflectedPropertyReader reader = new AssemblyReflectedPropertyReader();

            IEnumerable<KeyValuePair<string, Type>> fields = reader.GetFields(typeof(Assembly));
            fields = fields.Concat(reader.GetFields(typeof(AssemblyName), "Name/"));
            fields = fields.Concat(reader.GetFields(typeof(CultureInfo), "Culture/"));

            IDictionary<string, Type> d = fields.ToDictionary(key => key.Key, value => value.Value);
            d.Add("PublicKeyToken", typeof(string));
            d.Add("Assembly.Type", typeof(IList<IDictionary<string, object>>));

            return d;
        }

        public void InspectAssembly(string path, IEnumerable<string> selectProperties)
        {
            try
            {
                AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
                Assembly a = Assembly.ReflectionOnlyLoadFrom(path);

                ReflectedPropertyReader reader = new AssemblyReflectedPropertyReader();

                foreach (KeyValuePair<string, object> pair in reader.GetProperties(a.GetName(), null))
                    m_properties.Add("Name/" + pair.Key, pair.Value);

                foreach (KeyValuePair<string, object> pair in reader.GetProperties(a, selectProperties))
                    m_properties.Add(pair.Key, pair.Value);

                m_properties.Add("PublicKeyToken", BitConverter.ToString(a.GetName().GetPublicKeyToken()));

                foreach (KeyValuePair<string, object> pair in reader.GetProperties(a.GetName().CultureInfo, null))
                    m_properties.Add("Culture/" + pair.Key, pair.Value);

                TypePropertyReader typeReader = new TypePropertyReader();
                IList<IDictionary<string, object>> list = new List<IDictionary<string, object>>();
                foreach (Type t in a.GetTypes())
                    list.Add(typeReader.ReadObjectProperties(t));

                m_properties.Add("Assembly.Type", list);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            finally
            {
                AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
            }
        }

        Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
        {
            return Assembly.ReflectionOnlyLoad(args.Name);
        }

        public IEnumerable<KeyValuePair<string, object>> Properties
        {
            get
            {
                return m_properties;
            }
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions