Ejemplo n.º 1
0
        public override object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer)
        {
            object _returnVal;

            if (!_parseEnumAsArray && typeToParse.GetCustomAttribute <FlagsAttribute>() != null)
            {
                _parseEnumAsArray = true;
                Type        _enumArrayType = typeToParse.MakeArrayType();
                ITypeParser _arrayParser   = parserContainer.GetParser(_enumArrayType);
                Array       _enumArray     = _arrayParser.Parse(toParse, _enumArrayType, parserContainer) as Array;
                int         _totalVal      = 0;
                int         _iter          = 0;

                foreach (object enVal in _enumArray)
                {
                    if (_iter == 0)
                    {
                        _totalVal = (int)enVal;
                    }
                    else
                    {
                        _totalVal = _totalVal | (int)enVal;
                    }
                    _iter++;
                }
                _returnVal        = _totalVal;
                _parseEnumAsArray = false;
            }
            else
            {
                _returnVal = Enum.Parse(typeToParse, toParse, true);
            }
            return(_returnVal);
        }
Ejemplo n.º 2
0
        public object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer)
        {
            Type   _myUnderLyingType = Nullable.GetUnderlyingType(typeToParse);
            object _rtrnVal          = parserContainer.GetParser(_myUnderLyingType)?.Parse(toParse, _myUnderLyingType, parserContainer);

            return(_rtrnVal);
        }
Ejemplo n.º 3
0
        private object ParseElement(string toParse, Type type, ITypeParserContainer parserContainer)
        {
            ITypeParser _parser = parserContainer.GetParser(type);

            if (_parser == null)
            {
                throw new Exception($"Parsing type '{type.Name}' not handled");
            }
            return(_parser.Parse(toParse, type, parserContainer));
        }
Ejemplo n.º 4
0
        public void TestOverrideVirtualComplexProperty_ParamsObject()
        {
            //create and save new type
            object _newProduct = DynamicTypeCreator
                                 .Create <ParamsObject>("NewParams")
                                 .AddPassThroughCtors()
                                 .OverrideGet <ITypeParserContainer>("TypeParser",
                                                                     () => new TypeParserContainer(false, new KeyValueParser()))
                                 .FinishBuildingType()
                                 .GetConstructor(new Type[] { typeof(string[]) })
                                 .Invoke(new object[] { new string[0] {
                                                        } });

            PropertyInfo _meth = _newProduct.GetType().GetProperties().FirstOrDefault(m => m.Name == "TypeParser");

            Assert.IsNotNull(_meth, "new method not found");
            ITypeParserContainer _yis = (ITypeParserContainer)_meth.GetValue(_newProduct);
            bool _stringNotAccepted   = false;

            try
            {
                _yis.GetParser(typeof(string));
            }
            catch
            {
                _stringNotAccepted = true;
            }
            Assert.IsTrue(_stringNotAccepted, "String parser found");
            bool _keyValueParserFound = true;

            try
            {
                _yis.GetParser(typeof(KeyValuePair <,>));
            }
            catch
            {
                _keyValueParserFound = false;
            }
            Assert.IsTrue(_keyValueParserFound, "Keyvalue parser not found");
        }