Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CompMod / System / ComponentModel / basenumberconverter.cs / 1305376 / basenumberconverter.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
*/
namespace System.ComponentModel {
using Microsoft.Win32;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters;
using System.Security.Permissions;
///
/// Provides a base type converter for integral types.
///
[HostProtection(SharedState = true)]
public abstract class BaseNumberConverter : TypeConverter {
///
/// Determines whether this editor will attempt to convert hex (0x or #) strings
///
internal virtual bool AllowHex {
get {
return true;
}
}
///
/// The Type this converter is targeting (e.g. Int16, UInt32, etc.)
///
internal abstract Type TargetType {
get;
}
///
/// Convert the given value to a string using the given radix
///
internal abstract object FromString(string value, int radix);
///
/// Convert the given value to a string using the given formatInfo
///
internal abstract object FromString(string value, NumberFormatInfo formatInfo);
///
/// Convert the given value to a string using the given CultureInfo
///
internal abstract object FromString(string value, CultureInfo culture);
///
/// Create an error based on the failed text and the exception thrown.
///
internal virtual Exception FromStringError(string failedText, Exception innerException) {
return new Exception(SR.GetString(SR.ConvertInvalidPrimitive, failedText, TargetType.Name), innerException);
}
///
/// Convert the given value from a string using the given formatInfo
///
internal abstract string ToString(object value, NumberFormatInfo formatInfo);
///
/// Gets a value indicating whether this converter can convert an object in the
/// given source type to a 64-bit signed integer 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 value object to a 64-bit signed integer object.
///
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
string text = ((string)value).Trim();
try {
if (AllowHex && text[0] == '#') {
return FromString(text.Substring(1), 16);
}
else if (AllowHex && text.StartsWith("0x")
|| text.StartsWith("0X")
|| text.StartsWith("&h")
|| text.StartsWith("&H")) {
return FromString(text.Substring(2), 16);
}
else {
if (culture == null) {
culture = CultureInfo.CurrentCulture;
}
NumberFormatInfo formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo));
return FromString(text, formatInfo);
}
}
catch (Exception e) {
throw FromStringError(text, e);
}
}
return base.ConvertFrom(context, culture, value);
}
///
/// Converts the given value object to a 64-bit signed integer object using the
/// arguments.
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == null) {
throw new ArgumentNullException("destinationType");
}
if (destinationType == typeof(string) && value != null && TargetType.IsInstanceOfType(value)) {
if (culture == null) {
culture = CultureInfo.CurrentCulture;
}
NumberFormatInfo formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo));
return ToString(value, formatInfo);
}
if (destinationType.IsPrimitive) {
return Convert.ChangeType(value, destinationType, culture);
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type t) {
if (base.CanConvertTo(context, t) || t.IsPrimitive) {
return true;
}
return false;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
*/
namespace System.ComponentModel {
using Microsoft.Win32;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters;
using System.Security.Permissions;
///
/// Provides a base type converter for integral types.
///
[HostProtection(SharedState = true)]
public abstract class BaseNumberConverter : TypeConverter {
///
/// Determines whether this editor will attempt to convert hex (0x or #) strings
///
internal virtual bool AllowHex {
get {
return true;
}
}
///
/// The Type this converter is targeting (e.g. Int16, UInt32, etc.)
///
internal abstract Type TargetType {
get;
}
///
/// Convert the given value to a string using the given radix
///
internal abstract object FromString(string value, int radix);
///
/// Convert the given value to a string using the given formatInfo
///
internal abstract object FromString(string value, NumberFormatInfo formatInfo);
///
/// Convert the given value to a string using the given CultureInfo
///
internal abstract object FromString(string value, CultureInfo culture);
///
/// Create an error based on the failed text and the exception thrown.
///
internal virtual Exception FromStringError(string failedText, Exception innerException) {
return new Exception(SR.GetString(SR.ConvertInvalidPrimitive, failedText, TargetType.Name), innerException);
}
///
/// Convert the given value from a string using the given formatInfo
///
internal abstract string ToString(object value, NumberFormatInfo formatInfo);
///
/// Gets a value indicating whether this converter can convert an object in the
/// given source type to a 64-bit signed integer 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 value object to a 64-bit signed integer object.
///
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
string text = ((string)value).Trim();
try {
if (AllowHex && text[0] == '#') {
return FromString(text.Substring(1), 16);
}
else if (AllowHex && text.StartsWith("0x")
|| text.StartsWith("0X")
|| text.StartsWith("&h")
|| text.StartsWith("&H")) {
return FromString(text.Substring(2), 16);
}
else {
if (culture == null) {
culture = CultureInfo.CurrentCulture;
}
NumberFormatInfo formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo));
return FromString(text, formatInfo);
}
}
catch (Exception e) {
throw FromStringError(text, e);
}
}
return base.ConvertFrom(context, culture, value);
}
///
/// Converts the given value object to a 64-bit signed integer object using the
/// arguments.
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == null) {
throw new ArgumentNullException("destinationType");
}
if (destinationType == typeof(string) && value != null && TargetType.IsInstanceOfType(value)) {
if (culture == null) {
culture = CultureInfo.CurrentCulture;
}
NumberFormatInfo formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo));
return ToString(value, formatInfo);
}
if (destinationType.IsPrimitive) {
return Convert.ChangeType(value, destinationType, culture);
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type t) {
if (base.CanConvertTo(context, t) || t.IsPrimitive) {
return true;
}
return false;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- BulletedListEventArgs.cs
- FullTextBreakpoint.cs
- WizardStepCollectionEditor.cs
- RotateTransform3D.cs
- WindowsFormsLinkLabel.cs
- AggregateNode.cs
- GraphicsContext.cs
- PropertyCollection.cs
- UniqueConstraint.cs
- HttpRequest.cs
- OpacityConverter.cs
- ColorIndependentAnimationStorage.cs
- SID.cs
- PseudoWebRequest.cs
- SaveFileDialogDesigner.cs
- FontUnitConverter.cs
- designeractionlistschangedeventargs.cs
- UrlPath.cs
- WebBrowserContainer.cs
- PropertyPathWorker.cs
- DuplicateMessageDetector.cs
- Util.cs
- TryLoadRunnableWorkflowCommand.cs
- StickyNote.cs
- Math.cs
- ClientCultureInfo.cs
- EntityDataSourceWrapperPropertyDescriptor.cs
- CategoryAttribute.cs
- Validator.cs
- CqlIdentifiers.cs
- LocalizableResourceBuilder.cs
- Cursor.cs
- ScrollBarAutomationPeer.cs
- Int32AnimationBase.cs
- DurationConverter.cs
- ZoneButton.cs
- DataObjectCopyingEventArgs.cs
- XmlAtomicValue.cs
- HttpHandlerActionCollection.cs
- XmlObjectSerializerWriteContextComplexJson.cs
- XsltCompileContext.cs
- SetStoryboardSpeedRatio.cs
- ParallelTimeline.cs
- Vector3DCollectionConverter.cs
- IsolatedStorageFileStream.cs
- RegexCompilationInfo.cs
- FrameAutomationPeer.cs
- ISSmlParser.cs
- GroupBoxDesigner.cs
- FileDialog_Vista.cs
- CodeSnippetTypeMember.cs
- DPTypeDescriptorContext.cs
- SizeConverter.cs
- SolidColorBrush.cs
- EnumBuilder.cs
- XmlDocument.cs
- ErrorsHelper.cs
- pingexception.cs
- CustomTypeDescriptor.cs
- EdmProviderManifest.cs
- LiteralDesigner.cs
- HtmlContainerControl.cs
- HScrollProperties.cs
- UnknownBitmapEncoder.cs
- Baml2006ReaderFrame.cs
- CodeComment.cs
- TransformConverter.cs
- LogLogRecordHeader.cs
- panel.cs
- _NegoStream.cs
- MouseActionConverter.cs
- SafeSecurityHelper.cs
- MessageEncoderFactory.cs
- RuntimeConfigurationRecord.cs
- DockPattern.cs
- DrawingVisualDrawingContext.cs
- ResolveCriteriaApril2005.cs
- UnmanagedMemoryStream.cs
- XmlArrayItemAttribute.cs
- SessionStateItemCollection.cs
- PreloadedPackages.cs
- VectorAnimationUsingKeyFrames.cs
- RtfNavigator.cs
- DataSourceControlBuilder.cs
- RemotingConfigParser.cs
- WebMethodAttribute.cs
- SpinWait.cs
- FixedTextPointer.cs
- CompModHelpers.cs
- EventSetter.cs
- RecordConverter.cs
- SimpleRecyclingCache.cs
- AutoGeneratedFieldProperties.cs
- WpfPayload.cs
- XmlAnyElementAttributes.cs
- SoapExtensionStream.cs
- ResXResourceWriter.cs
- Vector3DAnimation.cs
- StrokeNode.cs