IConvertible.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System
  5. {
  6. // The IConvertible interface represents an object that contains a value. This
  7. // interface is implemented by the following types in the System namespace:
  8. // Boolean, Char, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64,
  9. // Single, Double, Decimal, DateTime, and String. The interface may
  10. // be implemented by other types that are to be considered values. For example,
  11. // a library of nullable database types could implement IConvertible.
  12. //
  13. // Note: The interface was originally proposed as IValue.
  14. //
  15. // The implementations of IConvertible provided by the System.XXX value classes
  16. // simply forward to the appropriate Value.ToXXX(YYY) methods (a description of
  17. // the Value class follows below). In cases where a Value.ToXXX(YYY) method
  18. // does not exist (because the particular conversion is not supported), the
  19. // IConvertible implementation should simply throw an InvalidCastException.
  20. [CLSCompliant(false)]
  21. public interface IConvertible
  22. {
  23. // Returns the type code of this object. An implementation of this method
  24. // must not return TypeCode.Empty (which represents a null reference) or
  25. // TypeCode.Object (which represents an object that doesn't implement the
  26. // IConvertible interface). An implementation of this method should return
  27. // TypeCode.DBNull if the value of this object is a database null. For
  28. // example, a nullable integer type should return TypeCode.DBNull if the
  29. // value of the object is the database null. Otherwise, an implementation
  30. // of this method should return the TypeCode that best describes the
  31. // internal representation of the object.
  32. TypeCode GetTypeCode();
  33. // The ToXXX methods convert the value of the underlying object to the
  34. // given type. If a particular conversion is not supported, the
  35. // implementation must throw an InvalidCastException. If the value of the
  36. // underlying object is not within the range of the target type, the
  37. // implementation must throw an OverflowException. The
  38. // IFormatProvider will be used to get a NumberFormatInfo or similar
  39. // appropriate service object, and may safely be null.
  40. bool ToBoolean(IFormatProvider provider);
  41. char ToChar(IFormatProvider provider);
  42. sbyte ToSByte(IFormatProvider provider);
  43. byte ToByte(IFormatProvider provider);
  44. short ToInt16(IFormatProvider provider);
  45. ushort ToUInt16(IFormatProvider provider);
  46. int ToInt32(IFormatProvider provider);
  47. uint ToUInt32(IFormatProvider provider);
  48. long ToInt64(IFormatProvider provider);
  49. ulong ToUInt64(IFormatProvider provider);
  50. float ToSingle(IFormatProvider provider);
  51. double ToDouble(IFormatProvider provider);
  52. decimal ToDecimal(IFormatProvider provider);
  53. DateTime ToDateTime(IFormatProvider provider);
  54. string ToString(IFormatProvider provider);
  55. object ToType(Type conversionType, IFormatProvider provider);
  56. }
  57. }