public static Char ToChar(this String source)
        {
            Char i = ' ';

            Char.TryParse(source, out i);
            return(i);
        }
Example #2
0
        private static bool TryParseAsInteger(EETypePtr enumEEType, String value, out Object result)
        {
            result = null;

            if (value.Length == 0)
            {
                return(false);
            }

            if (!(Char.IsDigit(value[0]) || value[0] == '+' || value[0] == '-'))
            {
                return(false);
            }
            RuntimeImports.RhCorElementType corElementType = enumEEType.CorElementType;

            unsafe
            {
                switch (corElementType)
                {
                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_BOOLEAN:
                {
                    Boolean v;
                    if (!Boolean.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR:
                {
                    Char v;
                    if (!Char.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I1:
                {
                    SByte v;
                    if (!SByte.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U1:
                {
                    Byte v;
                    if (!Byte.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I2:
                {
                    Int16 v;
                    if (!Int16.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U2:
                {
                    UInt16 v;
                    if (!UInt16.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I4:
                {
                    Int32 v;
                    if (!Int32.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U4:
                {
                    UInt32 v;
                    if (!UInt32.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I8:
                {
                    Int64 v;
                    if (!Int64.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U8:
                {
                    UInt64 v;
                    if (!UInt64.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                default:
                    throw new NotSupportedException();
                }
            }
        }
Example #3
0
        /// <summary>
        /// String to Char(字符串 转换成 无符号、数值、整数)
        /// </summary>
        /// <remarks>
        ///  2014-06-23 16:31 Created By iceStone
        /// </remarks>
        /// <param name="s">String</param>
        /// <param name="def">Default</param>
        /// <returns>Byte</returns>
        public static char ToChar(this string s, char def = default(char))
        {
            char result;

            return(Char.TryParse(s, out result) ? result : def);
        }