Exemple #1
0
 public override object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer)
 {
     if (BoolFalseValues.Contains(toParse?.ToLower().Trim()))
     {
         return(false);
     }
     else if (BoolTrueValues.Contains(toParse?.ToLower().Trim()))
     {
         return(true);
     }
     else
     {
         throw new Exception("Boolean value invalid");
     }
 }
        private object ParseElement(string toParse, Type type)
        {
            object myVal            = null;
            Type   myPropType       = type;
            Type   myUnderLyingType = myPropType;
            bool   isNullable       = myPropType.IsGenericType && myPropType.GetGenericTypeDefinition() == typeof(Nullable <>);

            if (isNullable)
            {
                myUnderLyingType = Nullable.GetUnderlyingType(myPropType);
            }
            if (myUnderLyingType.IsEnum)
            {
                if (!_parseEnumAsArray && myUnderLyingType.GetCustomAttribute <FlagsAttribute>() != null)
                {
                    _parseEnumAsArray = true;
                    Type  _enumArrayType = myUnderLyingType.MakeArrayType();
                    Array _enumArray     = ParseArray(toParse, _enumArrayType) 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++;
                    }
                    myVal             = _totalVal;
                    _parseEnumAsArray = false;
                }
                else
                {
                    myVal = Enum.Parse(myUnderLyingType, toParse, true);
                }
            }
            else if (typeof(System.Security.SecureString).Equals(myUnderLyingType))
            {
                var secure = new System.Security.SecureString();

                foreach (var v in toParse.ToCharArray())
                {
                    secure.AppendChar(v);
                }
                myVal = secure;
            }
            else if (typeof(KeyValuePair <,>).Name.Equals(myUnderLyingType.Name))
            {
                Type[]   _genericTypes = myUnderLyingType.GetGenericArguments();
                string[] _keyValArr    = toParse.Split(':');
                if (_keyValArr == null || _keyValArr.Length != 2)
                {
                    throw new Exception("Failed to parse key value pair");
                }
                object _key = Parse(_keyValArr[0], _genericTypes[0]);
                object _val = Parse(_keyValArr[1], _genericTypes[1]);
                if (_key == null || _val == null)
                {
                    throw new Exception("Failed to parse key value pair");
                }
                object[] _keyValPair = new object[] { _key, _val };
                myVal = myUnderLyingType.GetConstructor(_genericTypes).Invoke(_keyValPair);
            }
            else if (typeof(IConvertible).IsAssignableFrom(myUnderLyingType))
            {
                if (myUnderLyingType == typeof(bool))
                {
                    if (BoolFalseValues.Contains(toParse?.ToLower().Trim()))
                    {
                        return(false);
                    }
                    else if (BoolTrueValues.Contains(toParse?.ToLower().Trim()))
                    {
                        return(true);
                    }
                }
                else
                {
                    myVal = Convert.ChangeType(toParse, myUnderLyingType);
                }
            }
            else if (typeof(Type).IsAssignableFrom(myUnderLyingType))
            {
                IEnumerable <Type> _types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(asm => asm.GetTypes());
                myVal = _types.SkipWhile(t =>
                                         (t.GetCustomAttribute <TypeParamAttribute>() != null &&
                                          t.GetCustomAttribute <TypeParamAttribute>().Ignore))
                        .Where(t => t.MatchesAttributeValueOrName <TypeParamAttribute>(toParse, attr => (attr == null || string.IsNullOrWhiteSpace(attr.FriendlyName)) ? "" : attr.FriendlyName.ToLower())).FirstOrDefault();
            }
            return(myVal);
        }
        public object ParseElement(string toParse, Type type)
        {
            object myVal            = null;
            Type   myPropType       = type;
            Type   myUnderLyingType = myPropType;

            if (toParse != null)
            {
                toParse = toParse.ToLower();
            }
            bool isNullable = myPropType.IsGenericType && myPropType.GetGenericTypeDefinition() == typeof(Nullable <>);

            if (isNullable)
            {
                myUnderLyingType = Nullable.GetUnderlyingType(myPropType);
            }
            if (myUnderLyingType.IsEnum)
            {
                if (!_parseEnumAsArray && myUnderLyingType.GetCustomAttribute <FlagsAttribute>() != null)
                {
                    _parseEnumAsArray = true;
                    Type  _enumArrayType = myUnderLyingType.MakeArrayType();
                    Array _enumArray     = ParseArray(toParse, _enumArrayType) 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++;
                    }
                    myVal             = _totalVal;
                    _parseEnumAsArray = false;
                }
                else
                {
                    myVal = Enum.Parse(myUnderLyingType, toParse, true);
                }
            }
            else if (typeof(System.Security.SecureString).Equals(myUnderLyingType))
            {
                var secure = new System.Security.SecureString();

                foreach (var v in toParse.ToCharArray())
                {
                    secure.AppendChar(v);
                }
                myVal = secure;
            }
            else if (typeof(IConvertible).IsAssignableFrom(myUnderLyingType))
            {
                if (myUnderLyingType == typeof(bool))
                {
                    if (BoolFalseValues.Contains(toParse))
                    {
                        return(false);
                    }
                    else if (BoolTrueValues.Contains(toParse))
                    {
                        return(true);
                    }
                }
                else
                {
                    myVal = Convert.ChangeType(toParse, myUnderLyingType);
                }
            }
            else if (typeof(Type).IsAssignableFrom(myUnderLyingType))
            {
                IEnumerable <Type> _types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(asm => asm.GetTypes());
                myVal = _types.SkipWhile(t =>
                                         (t.GetCustomAttribute <TypeParamAttribute>() != null &&
                                          t.GetCustomAttribute <TypeParamAttribute>().Ignore))
                        .Where(t => t.MatchesAttributeValueOrName <TypeParamAttribute>(toParse, attr => (attr == null || string.IsNullOrWhiteSpace(attr.FriendlyName)) ? "" : attr.FriendlyName.ToLower())).FirstOrDefault();
            }
            return(myVal);
        }