Beispiel #1
0
 /// <summary>
 /// 将给定 <see cref="System.Object"/> 的类型更改为给定 <see cref="System.Type"/>。
 /// </summary>
 /// <param name="value">要更改为新 <see cref="System.Type"/> 的对象。</param>
 /// <param name="type"><paramref name="value"/> 将变成的新 <see cref="System.Type"/>。</param>
 /// <param name="culture">一个 <see cref="System.Globalization.CultureInfo"/> 实例,
 /// 用于控制数据类型的强制转换。</param>
 /// <returns>一个包含作为新类型的给定值的对象。</returns>
 public override object ChangeType(object value, Type type, CultureInfo culture)
 {
     if (allowCast)
     {
         return(JFConvertExt.ChangeType(value, type, culture));
     }
     else
     {
         return(JFConvertExt.ImplicitChangeType(value, type, culture));
     }
 }
Beispiel #2
0
        /// <summary>
        /// 将字符串中的转义字符转换为原始的字符。
        /// </summary>
        /// <param name="str">要转换的字符串。</param>
        /// <param name="unicodeOnly">是否只处理 Unicode 转义。</param>
        /// <param name="customEscape">自定义的转义字符。</param>
        /// <returns>转换后的字符串。</returns>
        private static string Unescape(this string str, bool unicodeOnly, ISet <char> customEscape)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(str);
            }
            int idx = str.IndexOf('\\');

            if (idx < 0)
            {
                return(str);
            }
            int           len = str.Length, start = 0;
            StringBuilder builder = new StringBuilder(len);

            while (idx >= 0)
            {
                // 添加当前 '\' 之前的字符串。
                if (idx > start)
                {
                    builder.Append(str, start, idx - start);
                    start = idx;
                }
                // 跳过 '\' 字符。
                idx++;
                if (idx >= len)
                {
                    break;
                }
                // Unicode 转义需要的十六进制字符的长度。
                int  hexLen = 0;
                char ch     = str[idx];
                switch (ch)
                {
                case 'x':
                    // \x 后面必须是 2 位。
                    hexLen = 2;
                    break;

                case 'u':
                    // \u 后面必须是 4 位。
                    hexLen = 4;
                    break;

                case 'U':
                    // \U 后面必须是 8 位。
                    hexLen = 8;
                    break;
                }
                if (hexLen > 0)
                {
                    // Unicode 反转义。
                    if (CheckHexLength(str, idx + 1, hexLen))
                    {
                        idx++;
                        int charNum = JFConvertExt.ToInt32(str.Substring(idx, hexLen), 16);
                        if (charNum < 0xFFFF)
                        {
                            // 单个字符。
                            builder.Append((char)charNum);
                        }
                        else
                        {
                            // 代理项对的字符。
                            builder.Append(char.ConvertFromUtf32(charNum & 0x1FFFFF));
                        }
                        idx = start = idx + hexLen;
                    }
                }
                else if (!unicodeOnly)
                {
                    // 其它反转义。
                    char result;
                    if (TryUnescape(str[idx], customEscape, out result))
                    {
                        builder.Append(result);
                        idx = start = idx + 1;
                    }
                }
                idx = str.IndexOf('\\', idx);
            }
            // 添加剩余的字符串。
            if (start < len)
            {
                builder.Append(str.Substring(start));
            }
            return(builder.ToString());
        }