Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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;
///
/// Provides a type converter to convert
/// objects to and from various other representations.
///
[HostProtection(SharedState = true)]
public class DateTimeConverter : TypeConverter {
///
/// 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 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 to the given destination type using the context.
///
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
if (destinationType == typeof(InstanceDescriptor)) {
return true;
}
return base.CanConvertTo(context, destinationType);
}
///
/// Converts the given value object to a
/// object.
///
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
/// using the arguments.
///
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);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// 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;
///
/// Provides a type converter to convert
/// objects to and from various other representations.
///
[HostProtection(SharedState = true)]
public class DateTimeConverter : TypeConverter {
///
/// 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 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 to the given destination type using the context.
///
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
if (destinationType == typeof(InstanceDescriptor)) {
return true;
}
return base.CanConvertTo(context, destinationType);
}
///
/// Converts the given value object to a
/// object.
///
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
/// using the arguments.
///
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);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- MethodCallExpression.cs
- RemotingHelper.cs
- MailWebEventProvider.cs
- DelegatingHeader.cs
- WindowsListViewSubItem.cs
- SectionRecord.cs
- AttachmentService.cs
- AnimationStorage.cs
- RootBuilder.cs
- ReadOnlyDictionary.cs
- XmlStringTable.cs
- HttpPostLocalhostServerProtocol.cs
- ProjectedSlot.cs
- DocobjHost.cs
- EventMappingSettingsCollection.cs
- LabelDesigner.cs
- VisualStyleTypesAndProperties.cs
- NGCSerializer.cs
- StringFreezingAttribute.cs
- Style.cs
- SchemaAttDef.cs
- ServiceOperationParameter.cs
- HelpInfo.cs
- HostingEnvironment.cs
- BufferedStream2.cs
- FontStretch.cs
- InternalPolicyElement.cs
- SqlDataSourceWizardForm.cs
- CapacityStreamGeometryContext.cs
- IPEndPointCollection.cs
- ToolBarPanel.cs
- ColumnMapTranslator.cs
- Scene3D.cs
- ModelVisual3D.cs
- LocalValueEnumerator.cs
- FormViewPageEventArgs.cs
- InvariantComparer.cs
- UrlRoutingModule.cs
- SafeTimerHandle.cs
- SoundPlayer.cs
- OrderedDictionary.cs
- ReadOnlyKeyedCollection.cs
- AppDomainEvidenceFactory.cs
- DrawingBrush.cs
- MdImport.cs
- Subtract.cs
- serverconfig.cs
- RegularExpressionValidator.cs
- Convert.cs
- QilGenerator.cs
- ParameterExpression.cs
- FrameworkElement.cs
- CustomAttributeBuilder.cs
- COM2ColorConverter.cs
- HeaderedContentControl.cs
- AccessedThroughPropertyAttribute.cs
- AttributeCollection.cs
- NamespaceExpr.cs
- XmlUrlResolver.cs
- Hash.cs
- Component.cs
- ColorConvertedBitmap.cs
- PerformanceCounter.cs
- HttpServerVarsCollection.cs
- MarshalByRefObject.cs
- XslCompiledTransform.cs
- ResourceExpression.cs
- QualifiedCellIdBoolean.cs
- ResolveDuplexCD1AsyncResult.cs
- HtmlInputCheckBox.cs
- X509CertificateClaimSet.cs
- RawKeyboardInputReport.cs
- TextEditorCopyPaste.cs
- PenThreadWorker.cs
- AstTree.cs
- ServerIdentity.cs
- SQLString.cs
- CodeSubDirectoriesCollection.cs
- GiveFeedbackEvent.cs
- WebPartConnectionsConnectVerb.cs
- QuadraticEase.cs
- ActiveXSite.cs
- followingquery.cs
- DiscardableAttribute.cs
- CustomErrorsSection.cs
- SecurityResources.cs
- ReadWriteSpinLock.cs
- CalloutQueueItem.cs
- StringPropertyBuilder.cs
- PackWebRequest.cs
- QuaternionIndependentAnimationStorage.cs
- ParameterInfo.cs
- StreamedFramingRequestChannel.cs
- PropertyPathWorker.cs
- IssuedTokenParametersElement.cs
- DataControlFieldsEditor.cs
- HttpRequest.cs
- CollectionViewGroupInternal.cs
- Light.cs
- ListChangedEventArgs.cs