Esempio n. 1
0
        /// <summary>从提供者和连接字符串猜测数据库处理器</summary>
        /// <param name="connStr"></param>
        /// <param name="provider"></param>
        /// <returns></returns>
        internal static Type GetProviderType(String connStr, String provider)
        {
            if (!String.IsNullOrEmpty(provider))
            {
                //Type type = XCodeService.ResolveType<IDatabase>(m => "" + m.Identity != "" && GetDefault((DatabaseType)m.Identity).Support(provider));
                //if (type != null) return type;
                foreach (var item in XCodeService.Container.ResolveAllMaps(typeof(IDatabase)))
                {
                    if ("" + item.Identity == "")
                    {
                        continue;
                    }

                    var db = item.Instance as IDatabase;
                    if (db != null && db.Support(provider))
                    {
                        return(item.ImplementType);
                    }
                }

                var type = TypeX.GetType(provider, true);
                if (type != null)
                {
                    XCodeService.Register <IDatabase>(type, provider);
                }
                return(type);
            }
            else
            {
                // 这里的默认值来自于上面Reg里面的最后那个
                return(XCodeService.ResolveType <IDatabase>(String.Empty));
            }
        }
Esempio n. 2
0
        private void StartAttachServers()
        {
            var dic = Config.GetConfigByPrefix("XAgent.AttachServer.");

            if (dic != null && dic.Count > 0)
            {
                // 实例化
                foreach (var item in dic)
                {
                    if (!item.Key.IsNullOrWhiteSpace() && !item.Value.IsNullOrWhiteSpace())
                    {
                        WriteLine("");
                        WriteLine("正在加载:{0} = {1}", item.Key, item.Value);
                        var type = TypeX.GetType(item.Value, true);
                        if (type != null)
                        {
                            var service = TypeX.CreateInstance(type) as IServer;
                            if (service != null)
                            {
                                AttachServers[item.Key] = service;
                            }
                        }
                    }
                }

                // 加载配置。【服务名.属性】的配置方式
                foreach (var item in AttachServers)
                {
                    if (item.Value != null)
                    {
                        var type = item.Value.GetType();
                        // 遍历所有属性,查找指定的设置项
                        foreach (var pi in type.GetProperties(true))
                        {
                            var    name  = String.Format("XAgent.{0}.{1}", item.Key, pi.Name);
                            Object value = null;
                            // 读取配置,并赋值
                            if (Config.TryGetConfig(name, pi.PropertyType, out value))
                            {
                                WriteLine("配置:{0} = {1}", name, value);
                                //PropertyInfoX.Create(pi).SetValue(item.Value, value);
                                item.Value.SetValue(pi, value);
                            }
                        }
                    }
                }

                // 启动
                foreach (var item in AttachServers)
                {
                    if (item.Value != null)
                    {
                        WriteLine("启动:{0}", item.Key);
                        item.Value.Start();
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 根据实体名称,获取此实体字段名称的数组List<string>
        /// </summary>
        /// <param name="typeName">完整实体名称, [命名空间.实体名称]</param>
        /// <returns></returns>
        public static List <string> GetEntityFieldList(this string tableName)
        {
            List <string> list = new List <string>();

            tableName = string.Format("{0}.{1}", ConfigHelper.GetAppSettings("EntitySpaceName"), tableName);
            TypeX  EntityType = TypeX.Create(TypeX.GetType(tableName, true));  //根据类的完整名称建立类的类型,用于动态创建类 如: Clump.Mobile.Models.NewsInfo
            Object objEntity  = EntityType.CreateInstance();                   //动态建立类的对象 实体类的对象Object objEntity = EntityType.CreateInstance(true);意思是在创建实体对象时 A a = new A(true)

            PropertyInfo[] props        = objEntity.GetType().GetProperties(); //获取此对象的,字段集合
            object         propertValue = String.Empty;

            foreach (PropertyInfo item in props)
            {
                list.Add(item.Name);
            }
            return(list);
        }
Esempio n. 4
0
        /// <summary>获取配置文件指定的处理器</summary>
        /// <returns></returns>
        static Dictionary <String, List <Type> > GetHandler()
        {
            NameValueCollection nvs = Config.AppSettings;

            if (nvs == null || nvs.Count < 1)
            {
                return(null);
            }

            Dictionary <String, List <Type> > dic = new Dictionary <String, List <Type> >();

            // 遍历设置项
            foreach (String appName in nvs)
            {
                // 必须以指定名称开始
                if (!appName.StartsWith(handlerKey, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                // 总线通道名称
                String name = appName.Substring(handlerKey.Length);

                String str = nvs[appName];
                if (String.IsNullOrEmpty(str))
                {
                    continue;
                }

                String[] ss = str.Split(new Char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                //List<Type> list = dic.ContainsKey(name) ? dic[name] : new List<Type>();
                List <Type> list = null;
                if (!dic.TryGetValue(name, out list))
                {
                    list = new List <Type>();
                    dic.Add(name, list);
                }
                foreach (String item in ss)
                {
                    Type type = TypeX.GetType(item, true);
                    list.Add(type);
                }
            }
            return(dic.Count > 0 ? dic : null);
        }
Esempio n. 5
0
        protected override void FixField(IDataColumn field, DataRow drColumn)
        {
            base.FixField(field, drColumn);

            // 字段标识
            Int64 flag = GetDataRowValue <Int64>(drColumn, "COLUMN_FLAGS");

            Boolean?isLong = null;

            Int32 id = 0;

            if (Int32.TryParse(GetDataRowValue <String>(drColumn, "DATA_TYPE"), out id))
            {
                DataRow[] drs = FindDataType(field, "" + id, isLong);
                if (drs != null && drs.Length > 0)
                {
                    String typeName = GetDataRowValue <String>(drs[0], "TypeName");
                    field.RawType = typeName;

                    if (TryGetDataRowValue <String>(drs[0], "DataType", out typeName))
                    {
                        field.DataType = TypeX.GetType(typeName);
                    }

                    // 修正备注类型
                    if (field.DataType == typeof(String) && drs.Length > 1)
                    {
                        isLong = (flag & 0x80) == 0x80;
                        drs    = FindDataType(field, "" + id, isLong);
                        if (drs != null && drs.Length > 0)
                        {
                            typeName      = GetDataRowValue <String>(drs[0], "TypeName");
                            field.RawType = typeName;
                        }
                    }
                }
            }

            //// 处理自增
            //if (field.DataType == typeof(Int32))
            //{
            //    //field.Identity = (flag & 0x20) != 0x20;
            //}
        }
Esempio n. 6
0
        /// <summary>加载配置</summary>
        protected virtual void LoadConfig()
        {
            var nvs = Config.GetConfigByPrefix(CONFIG_PREFIX);

            if (nvs == null || nvs.Count < 1)
            {
                return;
            }

            foreach (var item in nvs)
            {
                if (item.Value.IsNullOrWhiteSpace())
                {
                    continue;
                }

                var name = item.Key;
                if (name.IsNullOrWhiteSpace())
                {
                    continue;
                }

                var type = TypeX.GetType(name, true);
                if (type == null)
                {
                    XTrace.WriteLine("未找到对象容器配置{0}中的类型{1}!", item.Key, name);
                    continue;
                }

                var map = GetConfig(item.Value);
                if (map == null)
                {
                    continue;
                }

                if (XTrace.Debug)
                {
                    XTrace.WriteLine("为{0}配置注册{1},标识Identity={2},优先级Priority={3}!", type.FullName, map.TypeName, map.Identity, map.Priority);
                }

                Register(type, null, null, map.TypeName, map.Mode, map.Identity, map.Priority /*, map.Singleton*/);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// [OLD Code]返回一个新创建的实体对象,并给各个字段赋值
        /// </summary>
        /// <param name="typeName">完整实体名称, [命名空间.实体名称]</param>
        /// <param name="rval">表单集合字符串</param>
        /// <returns></returns>
        public static object GetFormEntity(this string rval, string typeName)
        {
            typeName = string.Format("{0}.{1}", ConfigHelper.GetAppSettings("EntitySpaceName"), typeName);
            TypeX  EntityType = TypeX.Create(TypeX.GetType(typeName, true));   //根据类的完整名称建立类的类型,用于动态创建类 如: Clump.Mobile.Models.NewsInfo
            Object objEntity  = EntityType.CreateInstance();                   //动态建立类的对象 实体类的对象Object objEntity = EntityType.CreateInstance(true);意思是在创建实体对象时 A a = new A(true)

            PropertyInfo[] props        = objEntity.GetType().GetProperties(); //获取此对象的,字段集合
            object         propertValue = String.Empty;

            foreach (PropertyInfo item in props)
            {
                propertValue = rval.GetFromValue(item.Name);
                if (!"".Equals(propertValue))
                {
                    //根据字段类型,转换格式
                    switch ((PropertyInfoX.Create(EntityType, item.Name)).Property.PropertyType.Name)
                    {
                    case "String": propertValue = Convert.ToString(propertValue); break;

                    case "Int64": propertValue = Convert.ToInt64(propertValue); break;

                    case "Int32": propertValue = Convert.ToInt32(propertValue); break;

                    case "Int16": propertValue = Convert.ToInt16(propertValue); break;

                    case "Byte": propertValue = Convert.ToByte(propertValue); break;

                    case "Single": propertValue = Convert.ToSingle(propertValue); break;

                    case "Double": propertValue = Convert.ToDouble(propertValue); break;

                    case "Boolean": propertValue = Convert.ToBoolean(propertValue); break;

                    case "DateTime": propertValue = Convert.ToDateTime(propertValue); break;

                    default: break;
                    }
                    PropertyInfoX.SetValue(objEntity, item.Name, propertValue);//给字段赋值
                }
            }
            PropertyInfoX.SetValue(objEntity, "CreateTime", DateTime.Now);//给CreateTime字段赋值,添加时间字段是每个数据表标配(未解决:MySql配置,暂时不能默认添加当前时间)
            return(objEntity);
        }
Esempio n. 8
0
        /// <summary>修正指定字段</summary>
        /// <param name="field">字段</param>
        /// <param name="drColumn">字段元数据</param>
        /// <param name="drDataType">字段匹配的数据类型</param>
        protected virtual void FixField(IDataColumn field, DataRow drColumn, DataRow drDataType)
        {
            String typeName = field.RawType;

            // 修正数据类型 +++重点+++
            if (TryGetDataRowValue <String>(drDataType, "DataType", out typeName))
            {
                field.DataType = TypeX.GetType(typeName);
            }

            // 修正长度为最大长度
            if (field.Length == 0)
            {
                Int32 n = 0;
                if (TryGetDataRowValue <Int32>(drDataType, "ColumnSize", out n))
                {
                    field.Length = n;
                    if (field.NumOfByte == 0)
                    {
                        field.NumOfByte = field.Length;
                    }
                }

                if (field.Length <= 0 && field.DataType == typeof(String))
                {
                    field.Length = Int32.MaxValue / 2;
                }
            }

            // 处理格式参数
            if (!String.IsNullOrEmpty(field.RawType) && !field.RawType.EndsWith(")"))
            {
                String param = GetFormatParam(field, drDataType);
                if (!String.IsNullOrEmpty(param))
                {
                    field.RawType += param;
                }
            }
        }
Esempio n. 9
0
        private TemplateItem ProcessDirective(Directive directive, TemplateItem item)
        {
            #region 包含include
            if (String.Equals(directive.Name, "include", StringComparison.OrdinalIgnoreCase))
            {
                String name = directive.GetParameter("name");
                // 可能采用了相对路径
                if (!File.Exists(name))
                {
                    name = Path.Combine(Path.GetDirectoryName(item.Name), name);
                }

                String       content = null;
                TemplateItem ti      = FindTemplateItem(name);
                if (ti != null)
                {
                    ti.Included = true;
                    content     = ti.Content;
                }
                else
                {
                    // 尝试读取文件
                    if (File.Exists(name))
                    {
                        ti         = new TemplateItem();
                        ti.Name    = name;
                        ti.Content = File.ReadAllText(name);
                        Templates.Add(ti);

                        ti.Included = true;
                        content     = ti.Content;
                    }
                }
                // 允许内容为空
                //if (String.IsNullOrEmpty(content)) throw new TemplateException(directive.Block, String.Format("加载模版[{0}]失败!", name));

                return(ti);
            }
            #endregion

            if (String.Equals(directive.Name, "assembly", StringComparison.OrdinalIgnoreCase))
            {
                String name = directive.GetParameter("name");
                if (!AssemblyReferences.Contains(name))
                {
                    AssemblyReferences.Add(name);
                }
            }
            else if (String.Equals(directive.Name, "import", StringComparison.OrdinalIgnoreCase))
            {
                item.Imports.Add(directive.GetParameter("namespace"));
            }
            else if (String.Equals(directive.Name, "template", StringComparison.OrdinalIgnoreCase))
            {
                if (!item.Processed)
                {
                    // 由模版指令指定类名
                    String name = directive.GetParameter("name");
                    if (!String.IsNullOrEmpty(name))
                    {
                        item.ClassName = name;
                    }

                    //item.BaseClassName = directive.GetParameter("inherits");
                    if (directive.TryGetParameter("inherits", out name))
                    {
                        item.BaseClassName = name;
                    }
                    item.Processed = true;
                }
                else
                {
                    throw new TemplateException(directive.Block, "多个模版指令!");
                }
            }
            else if (String.Equals(directive.Name, "var", StringComparison.OrdinalIgnoreCase))
            {
                String name = directive.GetParameter("name");
                String type = directive.GetParameter("type");

                if (item.Vars.ContainsKey(name))
                {
                    throw new TemplateException(directive.Block, "模版变量" + name + "已存在!");
                }

                Type ptype = TypeX.GetType(type, true);
                if (ptype == null)
                {
                    throw new TemplateException(directive.Block, "无法找到模版变量类型" + type + "!");
                }

                // 因为TypeX.GetType的强大,模版可能没有引用程序集和命名空间,甚至type位于未装载的程序集中它也会自动装载,所以这里需要加上
                ImportType(item, ptype);
                item.Vars.Add(name, ptype);
            }
            return(null);
        }
Esempio n. 10
0
        private static Type GetTypeInternal(String typeName)
        {
            var type = TypeX.GetType(typeName, true);

            if (type != null)
            {
                return(type);
            }

            var entities = LoadEntities();

            if (entities == null || entities.Count <= 0)
            {
                return(null);
            }

            var p = typeName.LastIndexOf(".");

            if (p >= typeName.Length - 1)
            {
                return(null);
            }

            // 记录命名空间,命名空间必须精确匹配
            var ns = "";

            // 先处理带有命名空间的
            if (p > 0)
            {
                foreach (var item in entities)
                {
                    if (item.FullName == typeName)
                    {
                        return(item);
                    }

                    // 同时按照不区分大小写查找,遍历完成后如果还没有找到,就返回不区分大小写查找的结果
                    if (type == null && typeName.EqualIgnoreCase(item.FullName))
                    {
                        type = item;
                    }
                }
                if (type != null)
                {
                    return(type);
                }

                // 去掉前面的命名空间,采用表名匹配
                ns       = typeName.Substring(0, p);
                typeName = typeName.Substring(p + 1);
            }

            foreach (var item in entities)
            {
                // 命名空间必须匹配,允许不区分大小写
                if (!String.IsNullOrEmpty(ns) && !ns.EqualIgnoreCase(item.Namespace))
                {
                    continue;
                }

                if (item.Name == typeName)
                {
                    return(item);
                }

                // 同时按照不区分大小写查找,遍历完成后如果还没有找到,就返回不区分大小写查找的结果
                if (type == null)
                {
                    if (typeName.EqualIgnoreCase(item.Name))
                    {
                        type = item;
                    }
                    else
                    {
                        // 有可能用于查找的是表名,而表名曾经被格式化(大小写、去前缀等)
                        var ti = TableItem.Create(item);
                        if (ti != null && ti.DataTable != null && typeName.EqualIgnoreCase(ti.TableName))
                        {
                            type = item;
                        }
                    }
                }
            }

            return(type);
        }