예제 #1
0
        public static object[] Deserialize(byte[] bsonData, AssemblyLoader assembly)
        {
            using (MemoryStream memoryStream = new MemoryStream(bsonData))
            {
                object[] methodParams = null;
                using (BsonReader reader = new BsonReader(memoryStream))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    BsonObject     bsonObject = serializer.Deserialize <BsonObject>(reader);

                    List <string> types = JsonConvert.DeserializeObject <List <string> >(bsonObject.Payload[0].ToString());

                    methodParams = new object[types.Count];
                    for (int i = 1; i < bsonObject.Payload.Length; i++)
                    {
                        Type objectType = bsonObject.Payload[i].GetType();
                        if (objectType == typeof(JArray))
                        {
                            string type = types[i - 1];
                            if (type.Contains("List"))
                            {
                                Type listType            = typeof(List <>);
                                Type genericType         = assembly.ResolveClass(ExtractGenerics(type));
                                Type constructedListType = listType.MakeGenericType(genericType);

                                methodParams[i - 1] = JsonConvert.DeserializeObject(bsonObject.Payload[i].ToString(), constructedListType);
                            }
                            else
                            {
                                //TODO: map and others collections!
                            }
                        }
                        else if (objectType != Type.GetType(types[i - 1]))
                        {
                            switch (types[i - 1])
                            {
                            case "System.Int32":
                                methodParams[i - 1] = Convert.ToInt32(bsonObject.Payload[i]);
                                break;

                            case "System.UInt32":
                                methodParams[i - 1] = Convert.ToUInt32(bsonObject.Payload[i]);
                                break;

                            case "System.Int16":
                                methodParams[i - 1] = Convert.ToUInt16(bsonObject.Payload[i]);
                                break;

                            case "System.UInt16":
                                methodParams[i - 1] = Convert.ToUInt16(bsonObject.Payload[i]);
                                break;

                            case "System.Double":
                                methodParams[i - 1] = Convert.ToDouble(bsonObject.Payload[i]);
                                break;

                            case "System.Single":
                                methodParams[i - 1] = Convert.ToSingle(bsonObject.Payload[i]);
                                break;
                            }
                        }
                        else
                        {
                            methodParams[i - 1] = bsonObject.Payload[i];
                        }
                    }
                }
                return(methodParams);
            }
        }
예제 #2
0
		public static object[] Deserialize(byte[] bsonData, AssemblyLoader assembly)
		{
			using (MemoryStream memoryStream = new MemoryStream(bsonData))
			{
				object[] methodParams = null;
				using (BsonReader reader = new BsonReader(memoryStream))
				{
					JsonSerializer serializer = new JsonSerializer();
					BsonObject bsonObject = serializer.Deserialize<BsonObject>(reader);

					List<string> types = JsonConvert.DeserializeObject<List<string>>(bsonObject.Payload[0].ToString());

					methodParams = new object[types.Count];
					for (int i = 1; i < bsonObject.Payload.Length; i++)
					{
						Type objectType = bsonObject.Payload[i].GetType();
						if (objectType == typeof(JArray))
						{
							string type = types[i - 1];
							if (type.Contains("List"))
							{
								Type listType = typeof(List<>);
								Type genericType = assembly.ResolveClass(ExtractGenerics(type));
								Type constructedListType = listType.MakeGenericType(genericType);

								methodParams[i - 1] = JsonConvert.DeserializeObject(bsonObject.Payload[i].ToString(), constructedListType);
							}
							else
							{
								//TODO: map and others collections!
							}
						}
						else if (objectType != Type.GetType(types[i - 1]))
						{
							switch (types[i - 1])
							{
								case "System.Int32":
									methodParams[i - 1] = Convert.ToInt32(bsonObject.Payload[i]);
									break;
								case "System.UInt32":
									methodParams[i - 1] = Convert.ToUInt32(bsonObject.Payload[i]);
									break;
								case "System.Int16":
									methodParams[i - 1] = Convert.ToUInt16(bsonObject.Payload[i]);
									break;
								case "System.UInt16":
									methodParams[i - 1] = Convert.ToUInt16(bsonObject.Payload[i]);
									break;
								case "System.Double":
									methodParams[i - 1] = Convert.ToDouble(bsonObject.Payload[i]);
									break;
								case "System.Single":
									methodParams[i - 1] = Convert.ToSingle(bsonObject.Payload[i]);
									break;
							}
						}
						else
						{
							methodParams[i - 1] = bsonObject.Payload[i];
						}
					}
				}
				return methodParams;
			}
		}