예제 #1
0
 /// <summary> 加载Setter
 /// </summary>
 public void LoadSetter()
 {
     if (Setter != PreSetter)
     {
         return;
     }
     lock (this)
     {
         if (Setter == PreSetter) //Setter未编译
         {
             if (!CanWrite)       //当前成员可写
             {
                 Setter = ErrorSetter;
             }
             else if (Field)
             {
                 Setter = Literacy.CreateSetter((FieldInfo)MemberInfo);
             }
             else
             {
                 Setter = Literacy.CreateSetter((PropertyInfo)MemberInfo);
             }
         }
     }
 }
예제 #2
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);
        }
예제 #3
0
        /// <summary> 加载Getter
        /// </summary>
        public void LoadGetter()
        {
            if (Getter != PreGetter)
            {
                return;
            }

            lock (this)
            {
                if (Getter == PreGetter) //Getter未编译
                {
                    if (!CanRead)        //当前对象不可读
                    {
                        Getter = ErrorGetter;
                    }
                    else if (Field)
                    {
                        Getter = Literacy.CreateGetter((FieldInfo)MemberInfo);
                    }
                    else
                    {
                        Getter = Literacy.CreateGetter((PropertyInfo)MemberInfo);
                    }
                }
            }
        }
예제 #4
0
        /// <summary> 从指定的 Type 创建新的 JsonType 对象,该方法必须保证类型公开的构造函数有且只有一个
        /// </summary>
        public JsonType(Type type)
            : this(type, 0)
        {
            _ctor = Literacy.CreateNewObject(type);
            if (_ctor == null)
            {
                if (TypeInfo.IsSpecialType)
                {
                    return;
                }
                var ctors = type.GetConstructors();
                switch (ctors.Length)
                {
                case 0:
                    _ctor = args => {
                        throw new TypeInitializationException(TypesHelper.DisplayName(type),
                                                              new MissingMethodException("当前类型没有构造函数"));
                    };
                    break;

                case 1:
                    _ctor = Literacy.CreateNewObject(ctors[0]);
                    break;

                default:
                    _ctor = args => {
                        throw new TypeInitializationException(TypesHelper.DisplayName(type),
                                                              new MethodAccessException("构造函数调用不明确"));
                    };
                    break;
                }
            }
        }
예제 #5
0
 /// <summary> 从指定的 Type 创建新的 JsonType 对象,并指定构造函数
 /// </summary>
 public JsonType(Type type, ConstructorInfo ctor)
     : this(type, 1)
 {
     if (ctor == null && TypeInfo.IsSpecialType)
     {
         return;
     }
     AreNull(ctor, "ctor");
     AreTrue(type == ctor.ReflectedType, "ctor不属于当前类型");
     _ctor = Literacy.CreateNewObject(ctor);
 }
예제 #6
0
 /// <summary> 从指定的 Type 创建新的 JsonType 对象,并指定构造函数的参数
 /// </summary>
 public JsonType(Type type, Type[] ctorArgsType)
     : this(type, 2)
 {
     _ctor = Literacy.CreateNewObject(type, ctorArgsType);
     if (_ctor == null && TypeInfo.IsSpecialType)
     {
         return;
     }
     else
     {
         throw new ArgumentException("没有找到符合条件的构造函数");
     }
 }
예제 #7
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);
                }
            }
        }
예제 #8
0
        private JsonType(Type type, int i)
        {
            AreNull(type, "type");
            Type     = type;
            TypeInfo = TypesHelper.GetTypeInfo(type);
            _members = new Dictionary <string, JsonMember>(StringComparer.OrdinalIgnoreCase);
            var list = new List <JsonMember>();

            //枚举属性
            foreach (var p in Type.GetProperties())
            {
                var jm = JsonMember.Create(p);
                if (jm != null)
                {
                    AreTrue(_members.ContainsKey(jm.JsonName), "JsonName重复:" + jm.JsonName);
                    _members[jm.JsonName] = jm;
                    list.Add(jm);
                }
            }
            PropertyCount = list.Count;
            //枚举字段
            foreach (var p in Type.GetFields())
            {
                var jm = JsonMember.Create(p);
                if (jm != null)
                {
                    AreTrue(_members.ContainsKey(jm.JsonName), "JsonName重复:" + jm.JsonName);
                    _members[jm.JsonName] = jm;
                    list.Add(jm);
                }
            }
            Members = list.ToArray();
            //设置 TypeCodes ,ElementType ,KeyType
            switch (TypeCodes = TypeInfo.TypeCodes)
            {
            case TypeCodes.IListT:
                IsList = true;
                var args = SearchGenericInterface(type, typeof(IList <>)).GetGenericArguments();
                ElementType = JsonType.Get(args[0]);
                AddValue    = Literacy.CreateCaller(type.GetMethod("Add", args));
                break;

            case TypeCodes.IList:
                IsList = true;
                if (type.IsArray)
                {
                    ElementType = JsonType.Get(type.GetElementType());
                    AddValue    = (o, v) => ((System.Collections.ArrayList)o).Add(v[0]);
                }
                else
                {
                    ElementType = JsonType.Get(typeof(object));
                    AddValue    = (o, v) => ((System.Collections.IList)o).Add(v[0]);
                }
                break;

            case TypeCodes.IDictionary:
                IsDictionary = true;
                KeyType      = JsonType.Get(typeof(object));
                ElementType  = KeyType;
                AddKeyValue  = (o, v) => { ((System.Collections.IDictionary)o).Add(v[0], v[1]); return(null); };
                break;

            case TypeCodes.IDictionaryT:
                IsDictionary = true;
                var dictType = SearchGenericInterface(type, typeof(IDictionary <,>));
                args        = dictType.GetGenericArguments();
                KeyType     = JsonType.Get(args[0]);
                ElementType = JsonType.Get(args[1]);
                AddKeyValue = Literacy.CreateCaller(dictType.GetMethod("Add", args), type);
                break;

            default:
                break;
            }
        }