• 分析 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;
            }
        }
    }

    alarm   作者:NewSea     出处:http://newsea.cnblogs.com/    QQ,MSN:iamnewsea@hotmail.com

      如无特别标记说明,均为NewSea原创,版权私有,翻载必纠。欢迎交流,转载,但要在页面明显位置给出原文连接。谢谢。
  • 相关阅读:
    Ubuntu下一个openldapserver部署步骤
    秀球技:倒和其他无用
    POJ1201-Intervals(差动限制)
    Scrapy研究和探索(七)——如何防止被ban大集合策略
    word 一些有用的技巧
    Java设计模式偷跑系列(十二)组合模式建模和实现
    Sql使用视图(简单的视图)适合入门-level
    cocos2d 缓存池 对象的再利用
    txt 开关 csv 可通用 工具
    使用SharePoint创建和定义自己的网站页面
  • 原文地址:https://www.cnblogs.com/newsea/p/1316329.html
Copyright © 2020-2023  润新知