Exemplo n.º 1
0
        public TResponse Get <TResponse>(string relativeOrAbsoluteUrl)
        {
            httpReq.HttpMethod = HttpMethod.Get;

            var requestTypeName = typeof(TResponse).Namespace + "." + relativeOrAbsoluteUrl;
            var requestType     = typeof(TResponse).Assembly.GetType(requestTypeName);

            if (requestType == null)
            {
                throw new ArgumentException("Type not found: " + requestTypeName);
            }

            var request = ReflectionExtensions.CreateInstance(requestType);

            if (ApplyRequestFilters <TResponse>(request))
            {
                return(default(TResponse));
            }

            var response = ServiceManager.ServiceController.Execute(request,
                                                                    new HttpRequestContext(httpReq, httpRes, request, EndpointAttributes.HttpGet));

            if (ApplyResponseFilters <TResponse>(response))
            {
                return((TResponse)response);
            }

            return((TResponse)response);
        }
Exemplo n.º 2
0
        public static IDictionary <TKey, TValue> ParseDictionary <TKey, TValue>(
            string value, Type createMapType,
            ParseStringDelegate parseKeyFn, ParseStringDelegate parseValueFn)
        {
            if (value == null)
            {
                return(null);
            }

            var tryToParseItemsAsDictionaries =
                JsConfig.ConvertObjectTypesIntoStringDictionary && typeof(TValue) == typeof(object);

            var index = VerifyAndGetStartIndex(value, createMapType);

            var to = (createMapType == null)
                                ? new Dictionary <TKey, TValue>()
                                : (IDictionary <TKey, TValue>)ReflectionExtensions.CreateInstance(createMapType);

            if (value == JsWriter.EmptyMap)
            {
                return(to);
            }

            var valueLength = value.Length;

            while (index < valueLength)
            {
                var keyValue = Serializer.EatMapKey(value, ref index);
                Serializer.EatMapKeySeperator(value, ref index);
                var elementValue = Serializer.EatValue(value, ref index);

                var mapKey   = (TKey)parseKeyFn(keyValue);
                var mapValue = (TValue)parseValueFn(elementValue);

                if (tryToParseItemsAsDictionaries)
                {
                    var mapValueString            = mapValue as string;
                    var tryParseValueAsDictionary = JsonUtils.IsJsString(mapValueString);
                    if (tryParseValueAsDictionary)
                    {
                        var tmpMap = ParseDictionary <TKey, TValue>(mapValueString, createMapType, parseKeyFn, parseValueFn);
                        to[mapKey] = (tmpMap != null && tmpMap.Count > 0)
                                                        ? (TValue)tmpMap
                                                        : to[mapKey] = mapValue;
                    }
                    else
                    {
                        to[mapKey] = mapValue;
                    }
                }
                else
                {
                    to[mapKey] = mapValue;
                }

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(to);
        }
Exemplo n.º 3
0
        private static ICollection <T> CreateAndPopulate <T>(Type ofCollectionType, T[] withItems)
        {
            if (ofCollectionType == null)
            {
                return(new List <T>(withItems));
            }

            var genericTypeDefinition = ofCollectionType.GetGenericTypeDefinition();

            if (genericTypeDefinition == typeof(HashSet <T>))
            {
                return(new HashSet <T>(withItems));
            }
            if (genericTypeDefinition == typeof(LinkedList <T>))
            {
                return(new LinkedList <T>(withItems));
            }

            var collection = (ICollection <T>)ReflectionExtensions.CreateInstance(ofCollectionType);

            foreach (var item in withItems)
            {
                collection.Add(item);
            }
            return(collection);
        }
        public static IDictionary <TKey, TValue> ParseDictionary <TKey, TValue>(
            string value, Type createMapType,
            ParseStringDelegate parseKeyFn, ParseStringDelegate parseValueFn)
        {
            var index = VerifyAndGetStartIndex(value, createMapType);

            var to = (createMapType == null)
                        ? new Dictionary <TKey, TValue>()
                        : (IDictionary <TKey, TValue>)ReflectionExtensions.CreateInstance(createMapType);

            if (value == JsWriter.EmptyMap)
            {
                return(to);
            }

            var valueLength = value.Length;

            while (index < valueLength)
            {
                var keyValue = Serializer.EatMapKey(value, ref index);
                Serializer.EatMapKeySeperator(value, ref index);
                var elementValue = Serializer.EatValue(value, ref index);

                var mapKey   = (TKey)parseKeyFn(keyValue);
                var mapValue = (TValue)parseValueFn(elementValue);

                to[mapKey] = mapValue;

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(to);
        }
Exemplo n.º 5
0
        public virtual TFolder Create(string path, IEnumerable <string> filePaths)
        {
            TFolder folder = null;

            if (path == null)
            {
                folder = ReflectionExtensions.CreateInstance <TFolder>(true);
                if (filePaths != null)
                {
                    folder.AddRange(filePaths);
                }
            }
            else
            {
                try
                {
                    folder = ReflectionExtensions.CreateInstance <TFolder>(path, filePaths);
                }
                catch (Exception)
                {
                    folder = ReflectionExtensions.CreateInstance <TFolder>(path);
                    if (filePaths != null)
                    {
                        folder.AddRange(filePaths);
                    }
                }
            }
            return(folder);
        }
Exemplo n.º 6
0
        public Message EmptyResponse(Message requestMsg, Type requestType)
        {
            var responseType = AssemblyUtils.FindType(requestType.FullName + "Response");
            var response     = ReflectionExtensions.CreateInstance(responseType ?? typeof(object));

            return(requestMsg.Headers.Action == null
                ? Message.CreateMessage(requestMsg.Version, null, response)
                : Message.CreateMessage(requestMsg.Version, requestType.Name + "Response", response));
        }
    public static void GetCreatedAt_ReturnsCorrectValue_SucceededState()
    {
        var state = ReflectionExtensions.CreateInstance <SucceededState>(
            null, default(long), default(long));

        var result = state.GetCreatedAt();

        Assert.Equal(state.SucceededAt, result);
    }
    public static void GetCreatedAt_ReturnsCorrectValue_ProcessingState()
    {
        var state = ReflectionExtensions.CreateInstance <ProcessingState>(
            "serverId", "workerId");

        var result = state.GetCreatedAt();

        Assert.Equal(state.StartedAt, result);
    }
Exemplo n.º 9
0
        public static IRegTypeParser GetRegTypeParser(string type)
        {
            regTypeParserDic = new Dictionary <string, string>();
            TextAsset          ta         = AssetDatabase.LoadAssetAtPath <TextAsset>("Assets/LightFramework/Editor/Config/RegTypePasers/parsers.json");
            RegTypeParsersJson jsonObject = JsonUtility.FromJson <RegTypeParsersJson>(ta.text);

            foreach (RegTypeParserJson info in jsonObject.parserList)
            {
                // regTypeParserDic.Add(info.Type, info.ParserName);
                if (info.Type.Equals(type))
                {
                    var ob = ReflectionExtensions.CreateInstance(info.ParserName) as IRegTypeParser;
                    return(ob);
                }
            }
            return(null);
        }
Exemplo n.º 10
0
 public static T CreateInstance <T>()
 {
     return((T)ReflectionExtensions.CreateInstance <T>());
 }
        internal static object StringToType(
            Type type,
            string strType,
            EmptyCtorDelegate ctorFn,
            Dictionary <string, TypeAccessor> typeAccessorMap)
        {
            var index = 0;

            if (strType == null)
            {
                return(null);
            }

            //if (!Serializer.EatMapStartChar(strType, ref index))
            if (strType[index++] != JsWriter.MapStartChar)
            {
                throw DeserializeTypeRef.CreateSerializationError(type, strType);
            }

            if (strType == JsWriter.EmptyMap)
            {
                return(ctorFn());
            }

            object instance = null;

            var strTypeLength = strType.Length;

            while (index < strTypeLength)
            {
                var propertyName = Serializer.EatMapKey(strType, ref index);

                //Serializer.EatMapKeySeperator(strType, ref index);
                index++;

                var propertyValueStr = Serializer.EatValue(strType, ref index);
                var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1 && propertyValueStr[0] == '_';

                if (possibleTypeInfo && propertyName == JsWriter.TypeAttr)
                {
                    var typeName = Serializer.ParseString(propertyValueStr);
                    instance = ReflectionExtensions.CreateInstance(typeName);
                    if (instance == null)
                    {
                        Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr);
                    }
                    else
                    {
                        //If __type info doesn't match, ignore it.
                        if (!type.IsInstanceOfType(instance))
                        {
                            instance = null;
                        }
                    }

                    //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                    if (index != strType.Length)
                    {
                        index++;
                    }

                    continue;
                }

                if (instance == null)
                {
                    instance = ctorFn();
                }

                TypeAccessor typeAccessor;
                typeAccessorMap.TryGetValue(propertyName, out typeAccessor);

                var propType = possibleTypeInfo ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null;
                if (propType != null)
                {
                    try
                    {
                        if (typeAccessor != null)
                        {
                            var parseFn       = Serializer.GetParseFn(propType);
                            var propertyValue = parseFn(propertyValueStr);
                            typeAccessor.SetProperty(instance, propertyValue);
                        }

                        //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                        if (index != strType.Length)
                        {
                            index++;
                        }

                        continue;
                    }
                    catch
                    {
                        if (JsConfig.ThrowOnDeserializationError)
                        {
                            throw;
                        }
                        else
                        {
                            Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueStr);
                        }
                    }
                }

                if (typeAccessor != null && typeAccessor.GetProperty != null && typeAccessor.SetProperty != null)
                {
                    try
                    {
                        var propertyValue = typeAccessor.GetProperty(propertyValueStr);
                        typeAccessor.SetProperty(instance, propertyValue);
                    }
                    catch
                    {
                        if (JsConfig.ThrowOnDeserializationError)
                        {
                            throw;
                        }
                        else
                        {
                            Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr);
                        }
                    }
                }

                //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                if (index != strType.Length)
                {
                    index++;
                }
            }

            return(instance);
        }
Exemplo n.º 12
0
        internal static object StringToType(
            Type type,
            string strType,
            EmptyCtorDelegate ctorFn,
            Dictionary <string, TypeAccessor> typeAccessorMap)
        {
            var index = 0;

            if (strType == null)
            {
                return(null);
            }

            //if (!Serializer.EatMapStartChar(strType, ref index))
            for (; index < strType.Length; index++)
            {
                var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c])
                {
                    break;
                }
            }                                                                                                                                                                                    //Whitespace inline
            if (strType[index++] != JsWriter.MapStartChar)
            {
                throw DeserializeTypeRef.CreateSerializationError(type, strType);
            }

            if (strType == JsWriter.EmptyMap)
            {
                return(ctorFn());
            }

            object instance = null;

            var strTypeLength = strType.Length;

            while (index < strTypeLength)
            {
                var propertyName = JsonTypeSerializer.ParseJsonString(strType, ref index);

                //Serializer.EatMapKeySeperator(strType, ref index);
                for (; index < strType.Length; index++)
                {
                    var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c])
                    {
                        break;
                    }
                }                                                                                                                                                                                        //Whitespace inline
                if (strType.Length != index)
                {
                    index++;
                }

                var propertyValueStr = Serializer.EatValue(strType, ref index);
                var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1 && propertyValueStr[0] == '_';

                if (possibleTypeInfo && propertyName == JsWriter.TypeAttr)
                {
                    var typeName = Serializer.ParseString(propertyValueStr);

                    instance = ReflectionExtensions.CreateInstance(typeName);
                    if (instance == null)
                    {
                        Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr);
                    }
                    else
                    {
                        //If __type info doesn't match, ignore it.
                        if (!type.GetTypeInfo().IsInstanceOfType(instance))
                        {
                            instance = null;
                        }
                    }

                    Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                    continue;
                }

                if (instance == null)
                {
                    instance = ctorFn();
                }

                TypeAccessor typeAccessor;
                typeAccessorMap.TryGetValue(propertyName, out typeAccessor);

                var propType = possibleTypeInfo ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null;
                if (propType != null)
                {
                    try
                    {
                        if (typeAccessor != null)
                        {
                            //var parseFn = Serializer.GetParseFn(propType);
                            var parseFn = JsonReader.GetParseFn(propType);

                            var propertyValue = parseFn(propertyValueStr);
                            typeAccessor.SetProperty(instance, propertyValue);
                        }

                        //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                        for (; index < strType.Length; index++)
                        {
                            var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c])
                            {
                                break;
                            }
                        }                                                                                                                                                                                                //Whitespace inline
                        if (index != strType.Length)
                        {
                            var success = strType[index] == JsWriter.ItemSeperator || strType[index] == JsWriter.MapEndChar;
                            index++;
                            if (success)
                            {
                                for (; index < strType.Length; index++)
                                {
                                    var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c])
                                    {
                                        break;
                                    }
                                }                                                                                                                                                                                                        //Whitespace inline
                            }
                        }

                        continue;
                    }
                    catch (Exception e) when(!(e is JsonSerializationException))
                    {
                        Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueStr);
                    }
                }

                if (typeAccessor != null && typeAccessor.GetProperty != null && typeAccessor.SetProperty != null)
                {
                    try
                    {
                        var propertyValue = typeAccessor.GetProperty(propertyValueStr);
                        typeAccessor.SetProperty(instance, propertyValue);
                    }
                    catch (Exception e) when(!(e is JsonSerializationException))
                    {
                        Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr);
                    }
                }

                //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                for (; index < strType.Length; index++)
                {
                    var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c])
                    {
                        break;
                    }
                }                                                                                                                                                                                        //Whitespace inline
                if (index != strType.Length)
                {
                    var success = strType[index] == JsWriter.ItemSeperator || strType[index] == JsWriter.MapEndChar;
                    index++;
                    if (success)
                    {
                        for (; index < strType.Length; index++)
                        {
                            var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c])
                            {
                                break;
                            }
                        }                                                                                                                                                                                                //Whitespace inline
                    }
                }
            }

            return(instance);
        }
Exemplo n.º 13
0
        private static object StringToType(Type type, string strType,
                                           EmptyCtorDelegate ctorFn,
                                           IDictionary <string, SetPropertyDelegate> setterMap,
                                           IDictionary <string, ParseStringDelegate> parseStringFnMap)
        {
            var index = 0;

            if (strType == null)
            {
                return(null);
            }

            if (!Serializer.EatMapStartChar(strType, ref index))
            {
                throw new SerializationException(string.Format(
                                                     "Type definitions should start with a '{0}', expecting serialized type '{1}', got string starting with: {2}",
                                                     JsWriter.MapStartChar, type.Name, strType.Substring(0, strType.Length < 50 ? strType.Length : 50)));
            }

            if (strType == JsWriter.EmptyMap)
            {
                return(ctorFn());
            }

            object instance = null;
            string propertyName;
            ParseStringDelegate parseStringFn;
            SetPropertyDelegate setterFn;

            var strTypeLength = strType.Length;

            while (index < strTypeLength)
            {
                propertyName = Serializer.EatMapKey(strType, ref index);

                Serializer.EatMapKeySeperator(strType, ref index);

                var propertyValueString = Serializer.EatValue(strType, ref index);

                if (propertyName == JsWriter.TypeAttr)
                {
                    var typeName = Serializer.ParseString(propertyValueString);
                    instance = ReflectionExtensions.CreateInstance(typeName);
                    if (instance == null)
                    {
                        Tracer.Instance.WriteWarning("Could not find type: " + propertyValueString);
                    }
                    else
                    {
                        //If __type info doesn't match, ignore it.
                        if (!type.IsAssignableFrom(instance.GetType()))
                        {
                            instance = null;
                        }
                    }

                    Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                    continue;
                }

                if (instance == null)
                {
                    instance = ctorFn();
                }

                var propType = ExtractType(propertyValueString);
                if (propType != null)
                {
                    try
                    {
                        var parseFn       = Serializer.GetParseFn(propType);
                        var propertyValue = parseFn(propertyValueString);

                        setterMap.TryGetValue(propertyName, out setterFn);

                        if (setterFn != null)
                        {
                            setterFn(instance, propertyValue);
                        }

                        Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                        continue;
                    }
                    catch (Exception)
                    {
                        Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueString);
                    }
                }

                parseStringFnMap.TryGetValue(propertyName, out parseStringFn);

                if (parseStringFn != null)
                {
                    try
                    {
                        var propertyValue = parseStringFn(propertyValueString);

                        setterMap.TryGetValue(propertyName, out setterFn);

                        if (setterFn != null)
                        {
                            setterFn(instance, propertyValue);
                        }
                    }
                    catch (Exception)
                    {
                        Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueString);
                    }
                }

                Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
            }

            return(instance);
        }