Пример #1
0
        private static bool TryWriteScalar(IIonWriter writer, object obj, Type type, IScalarWriter scalarWriter)
        {
            if (type.IsEnum)
            {
                var propValue = Enum.GetName(type, obj);
                writer.WriteSymbol(propValue);
                return(true);
            }

            if (type == typeof(string))
            {
                var propValue = (string)obj;
                writer.WriteString(propValue);
                return(true);
            }

            if (type == typeof(int))
            {
                var propValue = (int)obj;
                writer.WriteInt(propValue);
                return(true);
            }

            if (type == typeof(long))
            {
                var propValue = (long)obj;
                writer.WriteInt(propValue);
                return(true);
            }

            if (type == typeof(bool))
            {
                var propValue = (bool)obj;
                writer.WriteBool(propValue);
                return(true);
            }

            if (type == typeof(float))
            {
                var propValue = (float)obj;
                writer.WriteFloat(propValue);
                return(true);
            }

            if (type == typeof(double))
            {
                var propValue = (double)obj;
                writer.WriteFloat(propValue);
                return(true);
            }

            if (type == typeof(DateTime))
            {
                var propValue = (DateTime)obj;
                writer.WriteTimestamp(new Timestamp(propValue));
                return(true);
            }

            if (type == typeof(DateTimeOffset))
            {
                var propValue = (DateTimeOffset)obj;
                writer.WriteTimestamp(new Timestamp(propValue));
                return(true);
            }

            if (type == typeof(decimal))
            {
                var propValue = (decimal)obj;
                writer.WriteDecimal(propValue);
                return(true);
            }

            //try to see if we can write use the scalar writer
            if (scalarWriter != null)
            {
                var method        = scalarWriter.GetType().GetMethod(nameof(IScalarWriter.TryWriteValue));
                var genericMethod = method.MakeGenericMethod(type);
                return((bool)genericMethod.Invoke(scalarWriter, new[] { writer, obj }));
            }

            return(false);
        }