分析 OQL.NET 所引用到的,用反射找出 类定义.

2008年以前,最精致的ORM实现,没有之一,现在看,依然很美:

http://www.macrobject.com/en/oql.net/oql_demo.htm

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


//Macrobject.NObject.OQL.Select( dbo.sa.UserRole.Id , dbo.sa.UserRole.RoleID )
//    .From(dbo.sa.UserRole )
//    .Where(dbo.sa.UserRole.Id.In("abc") )
namespace ConsoleApplication1
{
 
    using Macrobject.NObject;
    using System.Reflection;
    using System.IO;


    class Program
    {
        public const string ThePath = @"E:\DfApp\MyData\ConsoleApplication1\src\";

        public static void Main(string[] args)
        {

            Assembly ab = Assembly.LoadFrom(@"C:\Program Files\Macrobject\OQL.NET\NET 2.0\Macrobject.NObject.dll");


            foreach (Type item in ab.GetTypes())
            {
                if (
                    item.FullName.Trim().EndsWith(".")
                    || item.FullName.Contains("\t")
                    || item.FullName.Contains("<")
                    || item.FullName.Contains("+")
                    ) continue;
                DoWith(item);
            }
        }

        private static void DoWith(Type TheEntityType)
        {
            if (TheEntityType.FullName == typeof(FieldInfoSet).FullName)
            {
                int i = 0;

            }
            string fi = ThePath + TheEntityType.FullName.Replace(".", @"\") + ".cs";
            string pa = fi.Substring(0, fi.LastIndexOf("\\"));

            if (Directory.Exists(pa) == false)
            {
                Directory.CreateDirectory(pa);
            }

            //if (File.Exists(fi) == false)
            //{
            //    File.Create(fi);
            //}

            StreamWriter sw = new StreamWriter(fi, true);

            sw.WriteLine(string.Format(@"namespace {0}
{{
    public {1} {2} {3}
    {{
"
                , TheEntityType.Namespace
                , TheEntityType.IsClass ? "class" : TheEntityType.IsEnum ? "enum" : TheEntityType.IsInterface ? "interface" : "struct"
                , TheEntityType.Name
                , TheEntityType.IsClass ? TheEntityType.BaseType == null ? "" : ":" + TheEntityType.BaseType.FullName : ""
                ));

            int TheItemed = 0;

            foreach (PropertyInfo pi in TheEntityType.GetProperties())
            {
                if (pi.Name == "Item")
                {
                    TheItemed++;
                    if (TheItemed > 1) continue;
                }

                sw.WriteLine(string.Format(@"
        {5} {4} {0} {1} {{ {2}{3} }}",
                     pi.PropertyType.FullName
                     , pi.Name
                     , "get;"
                     , pi.CanWrite ? "set;" : TheEntityType.IsInterface ? "set ;" : "private set ;"
                     , ""
                     , TheEntityType.IsClass ? "public" : ""
                     ));
            }

            foreach (MethodInfo mi in TheEntityType.GetMethods())
            {
                if (TheEntityType.IsEnum) continue;

                if (mi.Name.StartsWith("get_")) continue;
                if (mi.Name.StartsWith("set_")) continue;

                List<string> par = new List<string>();
                foreach (ParameterInfo abcd in mi.GetParameters())
                {
                    par.Add(abcd.ParameterType.FullName + " " + abcd.Name);
                }

                sw.WriteLine(string.Format(@"
        {5} {0} {1} {2} ( {3} ){4}",
                    mi.IsStatic ? "static" : ""
                    , mi.ReturnType == typeof(void) ? "void" : mi.ReturnType.FullName
                    , mi.Name
                    , string.Join(",", par.ToArray())
                    ,
                    TheEntityType.IsInterface ? ";" :
                     mi.ReturnType == typeof(void) ? "{ return  ; }" :
                    "{ return  " + GetDefaultExpres(mi.ReturnType) + "; }"
                    , TheEntityType.IsClass ? "public" : ""
                     ));
            }

            foreach (System.Reflection.FieldInfo filedInfo in TheEntityType.GetFields())
            {
                if (TheEntityType.IsEnum && filedInfo.Name != "value__")
                {
                    sw.WriteLine(string.Format(@"
        {0} ",
                   filedInfo.Name + ","

                     ));
                }

            }


            sw.WriteLine(@"
    }
}
");
            sw.Close();
        }


        /// <summary>
        /// 如果Value 为空,返回默认值.
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetDefaultExpres(Type type)
        {
            string TypeName = type.Name.ToLower();
            if (type.IsGenericType)
            {
                TypeName = type.GetGenericArguments()[0].Name.ToLower();
            }


            switch (TypeName)
            {
                case "bool":
                case "boolean":
                    return "false";
                case "int":
                case "int32":
                case "int64":
                case "int16":
                case "float":
                case "decimal":
                case "long":
                case "byte":

                case "double":
                    return "0";
                default:
                    break;
            }
            return "null";
        }

        private static bool Test(string s)
        {
            if (s.Length > 1) return true;

            return false;
        }
    }
}

你可能感兴趣的:(.net)