CharConverter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / CompMod / System / ComponentModel / CharConverter.cs / 1 / CharConverter.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

/* 
 */ 
namespace System.ComponentModel {
    using Microsoft.Win32; 
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Globalization;
    using System.Runtime.Serialization.Formatters; 
    using System.Runtime.Remoting;
    using System.Runtime.InteropServices; 
    using System.Security.Permissions; 

    ///  
    ///    Provides
    ///       a type converter to convert Unicode
    ///       character objects to and from various other representations.
    ///  
    [HostProtection(SharedState = true)]
    public class CharConverter : TypeConverter { 
 
        /// 
        ///    Gets a value indicating whether this converter can 
        ///       convert an object in the given source type to a Unicode character object using
        ///       the specified context.
        /// 
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { 
            if (sourceType == typeof(string)) {
                return true; 
            } 
            return base.CanConvertFrom(context, sourceType);
        } 

 		/// 
		///      Converts the given object to another type.
		///  
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 		{ 
			if (destinationType == typeof(string) && value is char) 
 			{
 				if ((char)value == (char)0) 
				{
 					return "";
				}
			} 

			return base.ConvertTo(context, culture, value, destinationType); 
 		} 

        ///  
        ///    Converts the given object to a Unicode character object.
        /// 
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
            if (value is string) { 
                string text = (string)value;
                if (text.Length > 1) { 
                    text = text.Trim(); 
                }
 
                if (text != null && text.Length > 0) {
                    if (text.Length != 1) {
                        throw new FormatException(SR.GetString(SR.ConvertInvalidPrimitive, text, "Char"));
                    } 
                    return text[0];
                } 
 
                return '\0';
            } 
            return base.ConvertFrom(context, culture, value);
        }
    }
} 



                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK