private void ProcessResponseStream(Stream responseStream)
 {
     StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
     JsonReader jsonReader = new JsonReader(reader, this.m_context);
     jsonReader.ReadArrayStart();
     Dictionary<string, object> dictionary = jsonReader.ReadObject() as Dictionary<string, object>;
     if (dictionary == null)
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     object obj;
     if (!dictionary.TryGetValue("SchemaVersion", out obj))
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     string text = obj as string;
     if (string.IsNullOrEmpty(text))
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     this.m_context.ServerSchemaVersion = new Version(text);
     if (!dictionary.TryGetValue("LibraryVersion", out obj))
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     string text2 = obj as string;
     if (string.IsNullOrEmpty(text2))
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     this.m_context.ServerLibraryVersion = new Version(text2);
     if (dictionary.TryGetValue("TraceCorrelationId", out obj))
     {
         this.m_context.SetTraceCorrelationId(obj as string);
     }
     if (!dictionary.TryGetValue("ErrorInfo", out obj))
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     object obj2 = obj;
     if (obj2 != null)
     {
         Dictionary<string, object> dictionary2 = obj2 as Dictionary<string, object>;
         if (dictionary2 == null)
         {
             throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
         }
         ServerException ex = ServerException.CreateFromErrorInfo(dictionary2);
         throw ex;
     }
     else
     {
         if (!ClientRuntimeContext.CanHandleResponseSchema(this.m_context.ServerSchemaVersion))
         {
             throw new ClientRequestException(Resources.GetString("CannotHandleServerResponseSchema", new object[]
             {
                 text
             }));
         }
         while (jsonReader.PeekTokenType() != JsonTokenType.ArrayEnd)
         {
             long num = jsonReader.ReadInt64();
             obj = null;
             if (this.m_queryIdToObjectMap.TryGetValue(num.ToString(CultureInfo.InvariantCulture), out obj) && obj != null)
             {
                 ClientObject clientObject = obj as ClientObject;
                 string scriptType = null;
                 if (clientObject != null && jsonReader.PeekTokenType() == JsonTokenType.ObjectStart && jsonReader.PeekObjectType(out scriptType))
                 {
                     Type typeFromScriptType = ScriptTypeMap.GetTypeFromScriptType(scriptType);
                     if (typeFromScriptType != null && typeFromScriptType != clientObject.GetType())
                     {
                         ClientObject clientObject2 = ScriptTypeMap.CreateObjectFromScriptType(scriptType, this.Context) as ClientObject;
                         if (clientObject2 != null)
                         {
                             clientObject.SetTypedObject(clientObject2);
                             obj = clientObject2;
                         }
                     }
                 }
                 IFromJson fromJson = obj as IFromJson;
                 if (fromJson != null && !fromJson.CustomFromJson(jsonReader))
                 {
                     fromJson.FromJson(jsonReader);
                 }
             }
             else
             {
                 jsonReader.ReadObject();
             }
         }
         return;
     }
 }
Ejemplo n.º 2
0
        public T CastTo <T>(ClientObject obj) where T : ClientObject
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            ClientAction.CheckActionParameterInContext(this, obj);
            Type typeFromHandle = typeof(T);

            //Edited for .NET Core
            //if (!typeof(ClientObject).IsAssignableFrom(typeFromHandle))
            if (!typeof(ClientObject).GetTypeInfo().IsAssignableFrom(typeFromHandle))
            {
                throw new ArgumentException();
            }
            if (obj.Context != this)
            {
                throw new InvalidOperationException();
            }
            T t;

            //Edited for .NET Core
            //if (typeFromHandle.IsAssignableFrom(obj.GetType()))
            if (typeFromHandle.GetTypeInfo().IsAssignableFrom(obj.GetType()))
            {
                t = (T)((object)Activator.CreateInstance(typeFromHandle, new object[]
                {
                    this,
                    obj.Path
                }));
                t.SetObjectDataFrom(obj);
                return(t);
            }
            //Edited for .NET Core
            //if (obj.ObjectData.AssociatedObject != null && typeFromHandle.IsAssignableFrom(obj.ObjectData.AssociatedObject.GetType()))
            if (obj.ObjectData.AssociatedObject != null && typeFromHandle.GetTypeInfo().IsAssignableFrom(obj.ObjectData.AssociatedObject.GetType()))
            {
                t = (T)((object)Activator.CreateInstance(typeFromHandle, new object[]
                {
                    this,
                    obj.Path
                }));
                t.SetObjectDataFrom(obj);
                return(t);
            }
            //Edited for .NET Core
            //if (!obj.GetType().IsAssignableFrom(typeFromHandle))
            if (!obj.GetType().GetTypeInfo().IsAssignableFrom(typeFromHandle))
            {
                throw ClientUtility.CreateArgumentException("type");
            }
            //Edited for .NET Core
            //if (obj.ObjectData.AssociatedObject != null && !obj.ObjectData.AssociatedObject.GetType().IsAssignableFrom(typeFromHandle))
            if (obj.ObjectData.AssociatedObject != null && !obj.ObjectData.AssociatedObject.GetType().GetTypeInfo().IsAssignableFrom(typeFromHandle))
            {
                throw ClientUtility.CreateArgumentException("type");
            }
            t = (T)((object)Activator.CreateInstance(typeFromHandle, new object[]
            {
                this,
                obj.Path
            }));
            t.SetObjectDataFrom(obj);
            object obj2;

            if (obj.ObjectData.AssociatedObject == null)
            {
                obj2 = obj;
            }
            else
            {
                obj2 = obj.ObjectData.AssociatedObject;
            }
            if (obj2 != null)
            {
                List <string> list = new List <string>();
                Dictionary <string, object> queryIdToObjectMap = this.PendingRequest.QueryIdToObjectMap;
                foreach (KeyValuePair <string, object> current in queryIdToObjectMap)
                {
                    if (object.ReferenceEquals(current.Value, obj2))
                    {
                        list.Add(current.Key);
                    }
                }
                foreach (string current2 in list)
                {
                    queryIdToObjectMap[current2] = t;
                }
                obj.ObjectData.AssociatedObject = t;
            }
            return(t);
        }