Exemple #1
0
            public object Load(byte[] buffer)
            {
                TreePackage package = TreePackage.Load(buffer);

                if (!package.Attributes.ContainsKey("Type"))
                {
                    return(null);
                }
                Type type = FastWrapper.GetWarpperType((string)package.Attributes["Type"]);

                if (type == null)
                {
                    throw new TypeMismatchException("未能找到类型“" + (string)package.Attributes["Type"] + "”");
                }
                //bool isAnonymousType = type.IsAnonymousType();
                //if (isAnonymousType) {
                //    ConstructorInfo ctorX = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic)[0];
                //    ParameterInfo[] @paramsX = ctorX.GetParameters();
                //    object[] argsX = new object[@paramsX.Length];
                //    for (int i = 0; i < @paramsX.Length; i++) {
                //        argsX[i] = package[@paramsX[i].Name];
                //    }
                //    return FastWrapper.CreateInstance(type, argsX);
                //}
                ConstructorInfo[] ctors = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance | BindingFlags.Instance);
                object[]          args  = null;
                if (ctors.Length > 0)
                {
                    ConstructorInfo ctor    = ctors[0];
                    ParameterInfo[] @params = ctor.GetParameters();
                    args = new object[@params.Length];
                    for (int i = 0; i < @params.Length; i++)
                    {
                        string key = null;
                        foreach (string k in package.Keys)
                        {
                            if (k.Equals(@params[i].Name, StringComparison.OrdinalIgnoreCase))
                            {
                                key = k;
                            }
                        }
                        args[i] = package[key];
                        package.Remove(key);
                    }
                }
                else
                {
                    args = new object[0];
                }
                object result = FastWrapper.CreateInstance(type, args);

                foreach (string key in package.Keys)
                {
                    PropertyInfo property = type.GetProperty(key, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                    if (property == null)
                    {
                        FieldInfo field = type.GetField(key, BindingFlags.Public | BindingFlags.Instance);
                        if (field != null)
                        {
                            field.SetValue(result, package[key]);
                        }
                        continue;
                    }

                    //try {
                    if (property.CanWrite)
                    {
                        property.SetValue(result, TypeExtensions.Convert(package[key], property.PropertyType), null);
                    }
                    //} catch(Exception e){
                    //    throw new Exception(string.Format("key={0},name={1},type:{2},{3}", key, property.Name, type.FullName,e.Message));
                    //}
                }
                return(result);
            }
            public object Load(byte[] buffer)
            {
                TreePackage package = TreePackage.Load(buffer);

                if (!package.Attributes.ContainsKey("Type"))
                {
                    return(null);
                }
                Type type = FastWrapper.GetWarpperType((string)package.Attributes["Type"]);

                if (type == null)
                {
                    CommonException.ThrowTypeMismatch("未能找到类型“" + (string)package.Attributes["Type"] + "”");
                }
                //bool isAnonymousType = type.IsAnonymousType();
                //if (isAnonymousType) {
                //    ConstructorInfo ctorX = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic)[0];
                //    ParameterInfo[] @paramsX = ctorX.GetParameters();
                //    object[] argsX = new object[@paramsX.Length];
                //    for (int i = 0; i < @paramsX.Length; i++) {
                //        argsX[i] = package[@paramsX[i].Name];
                //    }
                //    return FastWrapper.CreateInstance(type, argsX);
                //}
                ConstructorInfo[] ctors = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance | BindingFlags.Instance);
                object[]          args  = null;
                if (ctors.Length > 0)
                {
                    ConstructorInfo ctor    = ctors[0];
                    ParameterInfo[] @params = ctor.GetParameters();
                    args = new object[@params.Length];
                    for (int i = 0; i < @params.Length; i++)
                    {
                        string key = null;
                        foreach (string k in package.Keys)
                        {
                            if (k.Equals(@params[i].Name, StringComparison.OrdinalIgnoreCase) || k.EndsWith(@params[i].Name, StringComparison.OrdinalIgnoreCase))
                            {
                                key = k;
                            }
                        }
                        if (string.IsNullOrEmpty(key))
                        {
                            return(null);
                        }
                        args[i] = package[key];
                        package.Remove(key);
                    }
                }
                else
                {
                    args = new object[0];
                }
                object result = FastWrapper.CreateInstance(type, args);

                foreach (string key in package.Keys)
                {
                    FieldInfo field = type.GetField(key, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                    if (field != null)
                    {
                        field.SetValue(result, TypeExtensions.Convert(package[key], field.FieldType));
                    }
                }
                return(result);
            }