/// <summary>
 /// Writes an enum value.
 /// </summary>
 /// <param name="value">The value.</param>
 public void WriteValue(Enum value)
 {
     this.WriteValue(EwsUtilities.SerializeEnum(value));
 }
Example #2
0
        /// <summary>
        /// Try to convert object to a string.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="strValue">The string representation of value.</param>
        /// <returns>True if object was converted, false otherwise.</returns>
        /// <remarks>A null object will be "successfully" converted to a null string.</remarks>
        internal bool TryConvertObjectToString(object value, out string strValue)
        {
            strValue = null;
            bool converted = true;

            if (value != null)
            {
                // All value types should implement IConvertible. There are a couple of special cases
                // that need to be handled directly. Otherwise use IConvertible.ToString()
                IConvertible convertible = value as IConvertible;
                if (value.GetType().IsEnum)
                {
                    strValue = EwsUtilities.SerializeEnum((Enum)value);
                }
                else if (convertible != null)
                {
                    switch (convertible.GetTypeCode())
                    {
                    case TypeCode.Boolean:
                        strValue = EwsUtilities.BoolToXSBool((bool)value);
                        break;

                    case TypeCode.DateTime:
                        strValue = this.Service.ConvertDateTimeToUniversalDateTimeString((DateTime)value);
                        break;

                    default:
                        strValue = convertible.ToString(CultureInfo.InvariantCulture);
                        break;
                    }
                }
                else
                {
                    // If the value type doesn't implement IConvertible but implements IFormattable, use its
                    // ToString(format,formatProvider) method to convert to a string.
                    IFormattable formattable = value as IFormattable;
                    if (formattable != null)
                    {
                        // Null arguments mean that we use default format and default locale.
                        strValue = formattable.ToString(null, null);
                    }
                    else if (value is ISearchStringProvider)
                    {
                        // If the value type doesn't implement IConvertible or IFormattable but implements
                        // ISearchStringProvider convert to a string.
                        // Note: if a value type implements IConvertible or IFormattable we will *not* check
                        // to see if it also implements ISearchStringProvider. We'll always use its IConvertible.ToString
                        // or IFormattable.ToString method.
                        ISearchStringProvider searchStringProvider = value as ISearchStringProvider;
                        strValue = searchStringProvider.GetSearchString();
                    }
                    else if (value is byte[])
                    {
                        // Special case for byte arrays. Convert to Base64-encoded string.
                        strValue = Convert.ToBase64String((byte[])value);
                    }
                    else
                    {
                        converted = false;
                    }
                }
            }

            return(converted);
        }