Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
0
        /// <summary>查找指定字段指定类型的数据类型</summary>
        /// <param name="field">字段</param>
        /// <param name="typeName"></param>
        /// <param name="isLong"></param>
        /// <returns></returns>
        protected virtual DataRow[] FindDataType(IDataColumn field, String typeName, Boolean? isLong)
        {
            DataRow[] drs = null;
            try
            {
                drs = OnFindDataType(field, typeName, isLong);
            }
            catch { }
            if (drs != null && drs.Length > 0) return drs;

            // 把Guid映射到varchar(32)去
            if (typeName == typeof(Guid).FullName || typeName.EqualIgnoreCase("Guid"))
            {
                typeName = "varchar(32)";
                try
                {
                    drs = OnFindDataType(field, typeName, isLong);
                }
                catch { }
                if (drs != null && drs.Length > 0) return drs;
            }

            // 如果该类型无法识别,则去尝试使用最接近的高阶类型
            foreach (var item in FieldTypeMaps)
            {
                if (item.Key.FullName == typeName)
                {
                    try
                    {
                        drs = OnFindDataType(field, item.Value.FullName, isLong);
                    }
                    catch { }
                    if (drs != null && drs.Length > 0) return drs;
                }
            }
            return null;
        }
Ejemplo n.º 3
0
        /// <summary>设置参数。返回自身,方便链式写法。</summary>
        /// <param name="name">参数名</param>
        /// <param name="value">参数值</param>
        /// <returns></returns>
        public virtual IEntityAccessor SetConfig(String name, Object value)
        {
            if (name.EqualIgnoreCase(EntityAccessorOptions.AllFields)) AllFields = (Boolean)value;

            return this;
        }
Ejemplo n.º 4
0
        private static Assembly CompileInternal(String outputAssembly, IEnumerable<String> references, CodeDomProvider provider, CompilerErrorCollection Errors, Template tmp)
        {
            var options = new CompilerParameters();
            foreach (var str in references)
            {
                options.ReferencedAssemblies.Add(str);
            }
            options.WarningLevel = 4;

            CompilerResults results = null;
            if (Debug)
            {
                //var sb = new StringBuilder();

                #region 调试状态,把生成的类文件和最终dll输出到XTemp目录下
                var tempPath = XTrace.TempPath;
                //if (!String.IsNullOrEmpty(outputAssembly)) tempPath = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(outputAssembly));
                if (!String.IsNullOrEmpty(outputAssembly) && !outputAssembly.EqualIgnoreCase(".dll")) tempPath = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(outputAssembly));

                //if (!String.IsNullOrEmpty(tempPath) && !Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);

                var srcpath = tempPath.CombinePath("src").EnsureDirectory(false);

                var files = new List<String>();
                foreach (var item in tmp.Templates)
                {
                    // 输出模版内容,为了调试使用
                    File.WriteAllText(tempPath.CombinePath(item.Name), item.Content);
                    if (item.Included) continue;

                    String name = item.Name.EndsWithIgnoreCase(".cs") ? item.Name : item.ClassName;
                    // 猜测后缀
                    Int32 p = name.LastIndexOf("_");
                    if (p > 0 && name.Length - p <= 5)
                        name = name.Substring(0, p) + "." + name.Substring(p + 1, name.Length - p - 1);
                    else if (!name.EndsWithIgnoreCase(".cs"))
                        name += ".cs";

                    //name = Path.Combine(tempPath, name);
                    name = srcpath.CombinePath(name);
                    File.WriteAllText(name, item.Source);

                    //sb.AppendLine(item.Source);
                    files.Add(name);
                }
                #endregion

                if (!String.IsNullOrEmpty(outputAssembly) && !outputAssembly.EqualIgnoreCase(".dll"))
                {
                    options.TempFiles = new TempFileCollection(tempPath, false);
                    options.OutputAssembly = Path.Combine(tempPath, outputAssembly);
                    options.GenerateInMemory = true;
                    options.IncludeDebugInformation = true;
                }

                results = provider.CompileAssemblyFromFile(options, files.ToArray());
                // 必须从内存字符串编译,否则pdb会定向到最终源代码文件
                //results = provider.CompileAssemblyFromSource(options, new String[] { sb.ToString() });
            }
            else
            {
                options.GenerateInMemory = true;

                results = provider.CompileAssemblyFromSource(options, tmp.Templates.Where(e => !e.Included).Select(e => e.Source).ToArray());
            }

            #region 编译错误处理
            if (results.Errors.Count > 0)
            {
                Errors.AddRange(results.Errors);

                var sb = new StringBuilder();
                CompilerError err = null;
                foreach (CompilerError error in results.Errors)
                {
                    error.ErrorText = error.ErrorText;
                    //if (String.IsNullOrEmpty(error.FileName)) error.FileName = inputFile;

                    if (!error.IsWarning)
                    {
                        String msg = error.ToString();
                        if (sb.Length < 1)
                        {
                            String code = null;
                            // 屏蔽因为计算错误行而导致的二次错误
                            try
                            {
                                code = tmp.FindBlockCode(error.FileName, error.Line);
                            }
                            catch { }
                            if (code != null)
                            {
                                msg += Environment.NewLine;
                                msg += code;
                            }
                            err = error;
                        }
                        else
                            sb.AppendLine();

                        sb.Append(msg);
                    }
                }
                if (sb.Length > 0)
                {
                    var ex = new TemplateException(sb.ToString());
                    ex.Error = err;
                    throw ex;
                }
            }
            else
            {
                try
                {
                    options.TempFiles.Delete();
                }
                catch { }
            }
            #endregion

            if (!results.Errors.HasErrors)
            {
                try
                {
                    return results.CompiledAssembly;
                }
                catch { }
            }
            return null;
        }