Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / CompMod / System / ComponentModel / DateTimeConverter.cs / 1 / DateTimeConverter.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /* */ namespace System.ComponentModel { using Microsoft.Win32; using System.ComponentModel.Design.Serialization; using System.Diagnostics; using System.Globalization; using System.Reflection; using System.Runtime.InteropServices; using System.Runtime.Remoting; using System.Runtime.Serialization.Formatters; using System.Security.Permissions; ////// [HostProtection(SharedState = true)] public class DateTimeConverter : TypeConverter { ///Provides a type converter to convert ////// objects to and from various other representations. /// 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 ////// object using the /// specified context. /// public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(InstanceDescriptor)) { return true; } return base.CanConvertTo(context, destinationType); } ///Gets a value indicating whether this converter can /// convert an object to the given destination type using the context. ////// public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string text = ((string)value).Trim(); if (text.Length == 0) { return DateTime.MinValue; } try { // See if we have a culture info to parse with. If so, then use it. // DateTimeFormatInfo formatInfo = null; if (culture != null ) { formatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); } if (formatInfo != null) { return DateTime.Parse(text, formatInfo); } else { return DateTime.Parse(text, culture); } } catch (FormatException e) { throw new FormatException(SR.GetString(SR.ConvertInvalidPrimitive, (string)value, "DateTime"), e); } } return base.ConvertFrom(context, culture, value); } ///Converts the given value object to a ////// object. /// public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value is DateTime) { DateTime dt = (DateTime) value; if (dt == DateTime.MinValue) { return string.Empty; } if (culture == null) { culture = CultureInfo.CurrentCulture; } DateTimeFormatInfo formatInfo = null; formatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); string format; if (culture == CultureInfo.InvariantCulture) { if (dt.TimeOfDay.TotalSeconds == 0) { return dt.ToString("yyyy-MM-dd", culture); } else { return dt.ToString(culture); } } if (dt.TimeOfDay.TotalSeconds == 0) { format = formatInfo.ShortDatePattern; } else { format = formatInfo.ShortDatePattern + " " + formatInfo.ShortTimePattern; } return dt.ToString(format, CultureInfo.CurrentCulture); } if (destinationType == typeof(InstanceDescriptor) && value is DateTime) { DateTime dt = (DateTime)value; if (dt.Ticks == 0) { // Make a special case for the empty DateTime // ConstructorInfo ctr = typeof(DateTime).GetConstructor(new Type[] {typeof(Int64)}); if (ctr != null) { return new InstanceDescriptor(ctr, new object[] { dt.Ticks }); } } ConstructorInfo ctor = typeof(DateTime).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int)}); if (ctor != null) { return new InstanceDescriptor(ctor, new object[] { dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond}); } } return base.ConvertTo(context, culture, value, destinationType); } } }Converts the given value object to a ////// object /// using the arguments.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TemplateParser.cs
- DEREncoding.cs
- DragStartedEventArgs.cs
- RootDesignerSerializerAttribute.cs
- StructuredTypeEmitter.cs
- ResourceBinder.cs
- ChoiceConverter.cs
- SymbolPair.cs
- ThreadStaticAttribute.cs
- RTLAwareMessageBox.cs
- BaseValidator.cs
- OverloadGroupAttribute.cs
- MiniLockedBorderGlyph.cs
- BamlResourceContent.cs
- listitem.cs
- _TimerThread.cs
- documentsequencetextview.cs
- PiiTraceSource.cs
- DesignerResources.cs
- CoreSwitches.cs
- SingleObjectCollection.cs
- FontFamilyConverter.cs
- ItemsControlAutomationPeer.cs
- StringUtil.cs
- WebPartConnectionsEventArgs.cs
- XsdBuilder.cs
- DataObjectAttribute.cs
- CompilerGlobalScopeAttribute.cs
- TypeUtils.cs
- SingleAnimationBase.cs
- EncryptedType.cs
- PageCatalogPart.cs
- CustomErrorsSection.cs
- ScriptDescriptor.cs
- ApplicationManager.cs
- Function.cs
- Token.cs
- METAHEADER.cs
- MetafileHeaderWmf.cs
- CompilerGlobalScopeAttribute.cs
- QueryHandler.cs
- CreateUserWizard.cs
- ClockGroup.cs
- HttpConfigurationSystem.cs
- Utility.cs
- NotEqual.cs
- DelegatingConfigHost.cs
- CSharpCodeProvider.cs
- StructuredProperty.cs
- ProtocolsConfigurationHandler.cs
- XmlMemberMapping.cs
- RSAOAEPKeyExchangeFormatter.cs
- DbExpressionBuilder.cs
- SelectionWordBreaker.cs
- PeerObject.cs
- BuildProviderAppliesToAttribute.cs
- ServiceManager.cs
- SimpleApplicationHost.cs
- DataGridViewRowEventArgs.cs
- HwndStylusInputProvider.cs
- ITreeGenerator.cs
- Span.cs
- TemplatedWizardStep.cs
- OleCmdHelper.cs
- DataGridItemAutomationPeer.cs
- DataBinder.cs
- Rfc2898DeriveBytes.cs
- DBSchemaRow.cs
- MasterPageBuildProvider.cs
- AnnotationDocumentPaginator.cs
- ThicknessAnimation.cs
- DataGridViewCellConverter.cs
- HierarchicalDataSourceControl.cs
- DemultiplexingClientMessageFormatter.cs
- EntityDataSourceReferenceGroup.cs
- RelationshipSet.cs
- X509Utils.cs
- SamlAuthorizationDecisionStatement.cs
- ConfigurationElement.cs
- XmlSchemaValidator.cs
- Int32KeyFrameCollection.cs
- RowToFieldTransformer.cs
- XmlDocumentSerializer.cs
- ZipIOLocalFileHeader.cs
- ObjectAnimationBase.cs
- ConnectionsZone.cs
- SubpageParagraph.cs
- GenericEnumConverter.cs
- StringSorter.cs
- DurableEnlistmentState.cs
- ConstraintEnumerator.cs
- ScrollEventArgs.cs
- DataRowExtensions.cs
- SmuggledIUnknown.cs
- FormViewCommandEventArgs.cs
- ExpressionVisitor.cs
- FormViewRow.cs
- Attributes.cs
- XMLSchema.cs
- ShimAsPublicXamlType.cs