Пример #1
0
        /// <summary> 转为实体对象
        /// </summary>
        /// <typeparam name="T">实体对象类型</typeparam>
        public T To <T>()
        {
            CheckClosed();
            var lit = Literacy.Cache(typeof(T), true);
            var obj = lit.NewObject();

            if (_cols != null)
            {
                foreach (var col in _cols)
                {
                    var p = lit.Property[col.Key];
                    p.TrySetValue(obj, ChangeType(_reader[col.Value], p.OriginalType));
                }
            }
            else
            {
                foreach (var p in lit.Property)
                {
                    var index = _reader.GetOrdinal(p.Name);
                    if (index > -1)
                    {
                        p.TrySetValue(obj, ChangeType(_reader[index], p.OriginalType));
                    }
                }
            }
            return((T)obj);
        }
Пример #2
0
        /// <summary> 在页面中注册Ajax脚本
        /// </summary>
        /// <param name="page"></param>
        public static void Register(Page page)
        {
            if (page == null)
            {
                return;
            }
            if (page.Items.Contains("blqw.Ajax2"))
            {
                return;
            }
            page.Items.Add("blqw.Ajax2", true);
            page.Items.Add("blqw.Ajax2.Js", "");
            if (page.Request.Form["blqw.ajaxmethod"] != null)
            {
                string str;
                try
                {
                    var      name   = page.Request.Form["blqw.ajaxmethod"];
                    var      method = page.GetType().GetMethod(name, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
                    var      met    = Literacy.Cache(method);
                    var      data   = page.Request.Form["blqw.ajaxdata"];
                    var      p      = method.GetParameters();
                    object[] args   = null;
                    if (data != null)
                    {
                        var d = data.Split('\0');
                        args = new object[p.Length];

                        if (args.Length != d.Length)
                        {
                            throw new ArgumentException("该方法需要提供 " + p.Length + " 个参数!");
                        }
                        for (int i = 0; i < p.Length; i++)
                        {
                            var val = d[i].Replace("\0\0", "\0");
                            var pit = p[i].ParameterType;
                            try
                            {
                                if (Type.GetTypeCode(pit) == TypeCode.String)
                                {
                                    args[i] = val;
                                }
                                else if (val.Length > 2 && (val[0] == '[' || val[0] == '{'))
                                {
                                    args[i] = Json.ToObject(pit, val);
                                }
                                else
                                {
                                    if (pit.IsValueType)
                                    {
                                        var type = Nullable.GetUnderlyingType(pit);

                                        if (val.Length == 0)
                                        {
                                            if (type == null)
                                            {
                                                args[i] = Activator.CreateInstance(pit);
                                            }
                                        }
                                        else
                                        {
                                            args[i] = Convert.ChangeType(val, type ?? pit);
                                        }
                                    }
                                    else if (val.Length != 0)
                                    {
                                        args[i] = Convert.ChangeType(val, pit);
                                    }
                                }
                            }
                            catch (Exception mex)
                            {
                                throw new ArgumentException("<参数 " + (i + 1) + ">转换失败!无法将 {" + val + "} 转换为 " + p[i].ParameterType.Name + " 类型", mex);
                            }
                        }
                    }
                    else if (p.Length > 0)
                    {
                        throw new ArgumentException("该方法需要提供 " + p.Length + " 个参数!");
                    }

                    var obj = met(page, args);
                    if (method.ReturnType.Namespace == "System")
                    {
                        if (method.ReturnType == typeof(void))
                        {
                            str = "";
                        }
                        else if (method.ReturnType == typeof(string))
                        {
                            str = "d:" + Json.ToJsonString(obj.ToString());
                        }
                        else if (method.ReturnType.IsValueType)
                        {
                            str = "d:" + Json.ToJsonString(obj.ToString());
                        }
                        else
                        {
                            str = "d:" + Json.ToJsonString(obj);
                        }
                    }
                    else
                    {
                        str = "d:" + Json.ToJsonString(obj);
                    }
                    var ext = page.Items["blqw.Ajax2.Js"].ToString();
                    if (ext.Length > 0)
                    {
                        ext = "v:" + Json.ToJsonString(ext);
                        if (str.Length > 0)
                        {
                            str = ext + "," + str;
                        }
                        else
                        {
                            str = ext;
                        }
                    }
                }
                catch (Exception ex)
                {
                    str = "e:" + new JsError(ex).ToString();
                }
                page.Response.Clear();
                page.Response.Write("{" + str + "}");
                page.Response.End();
            }
            else
            {
                string js = (string)Cache.Get(page.GetType().FullName + "->js", key =>
                {
                    var t            = page.GetType();
                    StringBuilder sb = new StringBuilder();
                    foreach (var m in t.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
                    {
                        var attr = (AjaxMethodAttribute)Attribute.GetCustomAttribute(m, typeof(AjaxMethodAttribute));
                        if (attr != null)
                        {
                            var ps       = m.GetParameters();
                            var funcName = attr.FunctionName ?? m.Name;
                            if (funcName.IndexOf('.') > -1)
                            {
                                sb.Append(ConvertVarName(funcName));
                                sb.Append("=function(){return blqw.Ajax.Exec('");
                            }
                            else
                            {
                                sb.Append("function ");
                                sb.Append(funcName);
                                sb.Append("(){return blqw.Ajax.Exec('");
                            }
                            sb.Append(m.Name);
                            sb.Append("',arguments);}");
                            sb.AppendLine();
                        }
                    }
                    return(sb.ToString());
                });

                if (page.Form == null)
                {
                    page.Response.Write(
                        string.Concat(@"<script type=""text/javascript"">
//<![CDATA[
", JAVASCRIPT, js, @"
//]]>
</script>"));
                }
                else
                {
                    page.ClientScript.RegisterClientScriptBlock(typeof(void), Guid.NewGuid().ToString("N"), JAVASCRIPT + js, true);
                }
            }
        }