The Labs \ Source Viewer \ SSCLI \ System.ComponentModel \ StringConverter

  1. //------------------------------------------------------------------------------
  2. // <copyright file="StringConverter.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*
  16. */
  17. namespace System.ComponentModel
  18. {
  19.    
  20.     using System.Diagnostics;
  21.     using System.Globalization;
  22.     using System.Runtime.InteropServices;
  23.     using System.Runtime.Remoting;
  24.     using System.Runtime.Serialization.Formatters;
  25.     using System.Security.Permissions;
  26.    
  27.     /// <devdoc>
  28.     /// <para>Provides a type converter to convert string objects to and from various other
  29.     /// representations.</para>
  30.     /// </devdoc>
  31.     [HostProtection(SharedState = true)]
  32.     public class StringConverter : TypeConverter
  33.     {
  34.        
  35.         /// <devdoc>
  36.         /// <para>Gets a value indicating whether this converter can convert an object in the
  37.         /// given source type to a string using the specified context.</para>
  38.         /// </devdoc>
  39.         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  40.         {
  41.             if (sourceType == typeof(string)) {
  42.                 return true;
  43.             }
  44.             return base.CanConvertFrom(context, sourceType);
  45.         }
  46.        
  47.         /// <devdoc>
  48.         /// <para>Converts the specified value object to a string object.</para>
  49.         /// </devdoc>
  50.         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  51.         {
  52.             if (value is string) {
  53.                 return (string)value;
  54.             }
  55.             if (value == null) {
  56.                 return "";
  57.             }
  58.             return base.ConvertFrom(context, culture, value);
  59.         }
  60.     }
  61. }

Developer Fusion