Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / CompMod / System / ComponentModel / basenumberconverter.cs / 1 / 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; ////// [HostProtection(SharedState = true)] public abstract class BaseNumberConverter : TypeConverter { ///Provides a base type converter for integral types. ////// 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); ////// public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) { return true; } return base.CanConvertFrom(context, sourceType); } ///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 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); } catch { throw; } } return base.ConvertFrom(context, culture, value); } ///Converts the given value object to a 64-bit signed integer object. ////// 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; } } }Converts the given value object to a 64-bit signed integer object using the /// arguments. ///
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- StatusBarPanelClickEvent.cs
- DataObjectEventArgs.cs
- DataColumnMapping.cs
- SiteMapNodeCollection.cs
- ObjectResult.cs
- CodeCatchClauseCollection.cs
- ObjectMemberMapping.cs
- DesignerHierarchicalDataSourceView.cs
- ListViewDeletedEventArgs.cs
- BinaryObjectWriter.cs
- XPathAncestorQuery.cs
- Int32Rect.cs
- Operators.cs
- DbProviderFactoriesConfigurationHandler.cs
- SegmentInfo.cs
- WebRequestModuleElement.cs
- PeerCollaboration.cs
- XmlUTF8TextReader.cs
- AstNode.cs
- SHA384Managed.cs
- RoutedEvent.cs
- InvokeProviderWrapper.cs
- GridViewAutomationPeer.cs
- XmlLinkedNode.cs
- WorkflowOperationErrorHandler.cs
- ScrollItemPattern.cs
- BamlResourceContent.cs
- ContextMenu.cs
- DataContractJsonSerializer.cs
- InputEventArgs.cs
- _LazyAsyncResult.cs
- ObsoleteAttribute.cs
- ReverseInheritProperty.cs
- DependencyObjectType.cs
- PeerNameResolver.cs
- SemanticTag.cs
- ObjectDataSourceMethodEventArgs.cs
- ConfigXmlElement.cs
- KeyedHashAlgorithm.cs
- GridViewCancelEditEventArgs.cs
- TabPanel.cs
- RuntimeConfigLKG.cs
- TransformCryptoHandle.cs
- CodeTypeParameterCollection.cs
- TranslateTransform.cs
- UnsafeNativeMethods.cs
- UrlPath.cs
- StrongName.cs
- DataGridViewComboBoxCell.cs
- CategoryGridEntry.cs
- Crypto.cs
- SHA384Managed.cs
- DrawToolTipEventArgs.cs
- DbParameterHelper.cs
- FaultImportOptions.cs
- ObjectDataSourceChooseMethodsPanel.cs
- DocumentOrderComparer.cs
- MappedMetaModel.cs
- SendKeys.cs
- RuleRefElement.cs
- MappingSource.cs
- Transform3DGroup.cs
- ProfileSection.cs
- PerformanceCounterPermissionEntryCollection.cs
- ToolStripOverflow.cs
- PageFunction.cs
- TextSchema.cs
- TableColumnCollectionInternal.cs
- SynchronizationContext.cs
- TextDecoration.cs
- StringDictionaryCodeDomSerializer.cs
- ButtonFieldBase.cs
- XmlQueryOutput.cs
- PreProcessor.cs
- TypeForwardedToAttribute.cs
- ExpressionSelection.cs
- XmlSortKeyAccumulator.cs
- MouseWheelEventArgs.cs
- StubHelpers.cs
- ZipIORawDataFileBlock.cs
- Environment.cs
- GroupBoxDesigner.cs
- RequiredFieldValidator.cs
- ClientTargetCollection.cs
- SqlComparer.cs
- ScriptServiceAttribute.cs
- _NativeSSPI.cs
- PlainXmlDeserializer.cs
- TextAction.cs
- XmlAttributeOverrides.cs
- InstanceDescriptor.cs
- ListViewDeleteEventArgs.cs
- WebControl.cs
- BinaryReader.cs
- DataControlFieldCell.cs
- WindowsComboBox.cs
- LongValidatorAttribute.cs
- LostFocusEventManager.cs
- RegexNode.cs
- Bezier.cs