コード例 #1
0
        // ######## TYPE PROVIDED
        internal static IConfigurationItem CreateConfigurationItem(string key, string contentType, string content, Compression.ICompressor compressor)
        {
            if (contentType.StartsWith(typeof(string).FullName))
            {
                // #### STRING
                string item = null;
                try
                {
                    item = Common.StringHelper.UnTokenizeString((string)Convert.ChangeType(content, Type.GetType(contentType)), Common.StringHelper.DefaultTokenReplacementPairs);
                }
                catch
                {
                    throw new Exception($"Unable to convert content to type. Key={key}, Content={content}, Type={typeof(string).FullName}");
                }
                return(new ConfigurationItem(key, item ?? "", typeof(System.String).FullName));
            }
            else if (contentType.StartsWith(typeof(char).FullName))
            {
                // #### CHAR
                char ch = ' ';
                try
                {
                    ch = (char)Convert.ChangeType(content, Type.GetType(contentType));
                }
                catch
                {
                    throw new Exception($"Unable to convert content to type. Key={key}, Content={content}, Type={typeof(char).FullName}");
                }
                string item = Common.StringHelper.UnTokenizeString(ch.ToString(), Common.StringHelper.DefaultTokenReplacementPairs);
                if ((item != null) && (item.Length > 0))
                {
                    return(new ConfigurationItem(key, item[0], typeof(System.Char).FullName));
                }
                else
                {
                    return(new ConfigurationItem(key, item ?? "", typeof(System.Char).FullName));
                }
            }
            else if (contentType.StartsWith(typeof(Version).FullName))
            {
                // #### VERSION
                var originalContent = content;
                if (!content.Contains('.'))
                {
                    content = content + ".0";
                }
                if (!Version.TryParse(content, out Version vResult))
                {
                    throw new Exception($"Unable to convert content to type. Key={key}, Content={originalContent}, Type={typeof(Version).FullName}");
                }
                return(new ConfigurationItem(key, vResult, typeof(System.Version).FullName));
            }
            else if (contentType.StartsWith(typeof(DateTimeOffset).FullName))
            {
                // #### DATETIMEOFFSET
                if (DateTimeOffset.TryParse(content, out DateTimeOffset dtoResult))
                {
                    var newDateTime = new DateTimeOffset(dtoResult.DateTime, TimeSpan.Zero);
                    return(new ConfigurationItem(key, newDateTime, typeof(System.DateTimeOffset).FullName));
                }
                else
                {
                    if (DateTimeOffset.TryParse(content + "+00:00", out DateTimeOffset dt2Result))
                    {
                        return(new ConfigurationItem(key, dt2Result, typeof(System.DateTimeOffset).FullName));
                    }
                    else
                    {
                        //return new ConfigurationItem(key, DateTimeOffset.MinValue, typeof(System.DateTimeOffset).FullName);
                        throw new Exception($"Unable to convert content to type. Key={key}, Content={content}, Type={typeof(DateTimeOffset).FullName}");
                    }
                }
            }
            else if (contentType.StartsWith(typeof(DateTime).FullName))
            {
                // TODO: should this keep the DateTime data type as DateTime? Currently, it changes DateTime types to DateTimeOffset types.

                // #### DATETIME
                if (DateTimeOffset.TryParse(content, out DateTimeOffset dt1Result))
                {
                    var newDateTime = new DateTimeOffset(dt1Result.DateTime, TimeSpan.Zero);
                    return(new ConfigurationItem(key, newDateTime, typeof(System.DateTimeOffset).FullName));
                }
                else
                {
                    if (DateTimeOffset.TryParse(content + " +00:00", out DateTimeOffset dt2Result))
                    {
                        return(new ConfigurationItem(key, dt2Result, typeof(System.DateTimeOffset).FullName));
                    }
                    else
                    {
                        //return new ConfigurationItem(key, DateTimeOffset.MinValue, typeof(System.DateTimeOffset).FullName);
                        throw new Exception($"Unable to convert content to type. Key={key}, Content={content}, Type={typeof(DateTime).FullName}");
                    }
                }
            }
            else if (contentType.StartsWith(typeof(TimeSpan).FullName))
            {
                // #### TIMESPAN
                if (!TimeSpan.TryParse(content, out TimeSpan tsResult))
                {
                    throw new Exception($"Unable to convert content to type. Key={key}, Content={content}, Type={typeof(TimeSpan).FullName}");
                }
                return(new ConfigurationItem(key, tsResult, typeof(System.TimeSpan).FullName));
            }
            else if (contentType.StartsWith(typeof(Guid).FullName))
            {
                // #### GUID
                if (!Guid.TryParse(content, out Guid gResult))
                {
                    throw new Exception($"Unable to convert content to type. Key={key}, Content={content}, Type={typeof(Guid).FullName}");
                }
                return(new ConfigurationItem(key, gResult, typeof(System.Guid).FullName));
            }
            else if (contentType.StartsWith(typeof(Type).FullName))
            {
                // #### TYPE
                return(new ConfigurationItem(key, content, typeof(System.Type).FullName));
            }
            else if (contentType.StartsWith(typeof(byte[]).FullName))
            {
                // #### BYTE[]
                if (!string.IsNullOrWhiteSpace(content))
                {
                    try
                    {
                        byte[] baResult;
                        if (_UseHexPairStringForByteArrays_)
                        {
                            if (_CompressByteArrays_)
                            {
                                baResult = compressor.Decompress(Common.ByteArrayHelper.ConvertStringToByteArray(content, Common.ByteArrayHelper.ConversionBase.Hexadecimal));
                            }
                            else
                            {
                                baResult = Common.ByteArrayHelper.ConvertStringToByteArray(content, Common.ByteArrayHelper.ConversionBase.Hexadecimal);
                            }
                        }
                        else
                        {
                            if (_CompressByteArrays_)
                            {
                                baResult = compressor.Decompress(Convert.FromBase64String(content));
                            }
                            else
                            {
                                baResult = Convert.FromBase64String(content);
                            }
                        }
                        return(new ConfigurationItem(key, baResult, typeof(System.Byte[]).FullName));
                    }
                    catch
                    {
                        throw new Exception($"Unable to convert content to type. Key={key}, Content={content}, Type={typeof(byte[]).FullName}");
                    }
                }
                else
                {
                    return(new ConfigurationItem(key, new byte[0], typeof(System.Byte[]).FullName));
                }
            }
            else if (StartsWith(contentType, _KnownTypes_))
            {
                // #### KNOWN TYPE
                var    type = Type.GetType(contentType);
                object item = null;
                try
                {
                    item = Convert.ChangeType(content, type);
                }
                catch
                {
                    throw new Exception($"Unable to convert content to type. Key={key}, Content={content}, Type={type.FullName}");
                }
                return(new ConfigurationItem(key, item, type.FullName));
            }
            else
            {
                // TODO: check for C#-Style type names

                // #### TEST FOR C#-STYLE TYPE NAMES
            }
            // #### UNKNOWN TYPE
            return(new ConfigurationItem(key, content, contentType));
        }
コード例 #2
0
        internal static string ConvertItemObjectToString(IConfigurationItem item, Compression.ICompressor compressor)
        {
            string value = null;

            if (item.ValueType.StartsWith(typeof(string).FullName))
            {
                // #### String
                value = Common.StringHelper.TokenizeString(Convert.ToString(item.Value), Common.StringHelper.DefaultTokenReplacementPairs);
            }
            else if (item.ValueType.StartsWith(typeof(char).FullName))
            {
                // #### Char
                value = Common.StringHelper.TokenizeString(Convert.ToString(item.Value), Common.StringHelper.DefaultTokenReplacementPairs);
            }
            else if (item.ValueType.StartsWith(typeof(Version).FullName))
            {
                // #### Version
                var vv = (item.Value as Version);
                value = $"{Fix(vv.Major)}.{Fix(vv.Minor)}.{Fix(vv.Build)}.{Fix(vv.Revision)}";

                // ----------
                int Fix(int fixValue)
                {
                    if (fixValue < 0)
                    {
                        fixValue = 0;
                    }
                    return(fixValue);
                }
            }
            else if (item.ValueType.StartsWith(typeof(DateTimeOffset).FullName))
            {
                // #### DateTimeOffset
                value = ((DateTimeOffset)item.Value).ToString("yyyy/MM/dd HH:mm:ss.fff");
            }
            else if (item.ValueType.StartsWith(typeof(DateTime).FullName))
            {
                // #### DateTime
                value = ((DateTime)item.Value).ToString("yyyy/MM/dd HH:mm:ss.fff");
            }
            else if (item.ValueType.StartsWith(typeof(TimeSpan).FullName))
            {
                // #### TimeSpan
                value = ((TimeSpan)item.Value).ToString();
            }
            else if (item.ValueType.StartsWith(typeof(Guid).FullName))
            {
                // #### Guid
                value = ((Guid)item.Value).ToString("D");
            }
            else if (item.ValueType.StartsWith(typeof(Type).FullName))
            {
                // #### Type
                value = ((string)item.Value);
            }
            else if (item.ValueType.StartsWith(typeof(byte[]).FullName))
            {
                // #### byte[]
                if (_UseHexPairStringForByteArrays_)
                {
                    if (_CompressByteArrays_)
                    {
                        value = Common.ByteArrayHelper.ConvertByteArrayToString(compressor.Compress((byte[])item.Value), Common.ByteArrayHelper.ConversionBase.Hexadecimal);
                    }
                    else
                    {
                        value = Common.ByteArrayHelper.ConvertByteArrayToString((byte[])item.Value, Common.ByteArrayHelper.ConversionBase.Hexadecimal);
                    }
                }
                else
                {
                    if (_CompressByteArrays_)
                    {
                        value = Convert.ToBase64String(compressor.Compress((byte[])item.Value));
                    }
                    else
                    {
                        value = Convert.ToBase64String((byte[])item.Value);
                    }
                }
            }
            else
            {
                // #### Known Type
                value = Convert.ToString(item.Value);
            }
            //
            return(value);
        }