Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Core / CSharp / System / Windows / DurationConverter.cs / 1 / DurationConverter.cs
//------------------------------------------------------------------------------ // Microsoft Windows Client Platform // Copyright (c) Microsoft Corporation, 2004 // // File: DurationConverter.cs //----------------------------------------------------------------------------- using System; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Diagnostics; using System.Globalization; using System.Reflection; using System.Security; namespace System.Windows { ////// Provides a type converter to convert Duration to and from other representations. /// public class DurationConverter : TypeConverter { ////// CanConvertFrom - Returns whether or not this class can convert from a given type /// ///public override bool CanConvertFrom(ITypeDescriptorContext td, Type t) { if (t == typeof(string)) { return true; } else { return false; } } /// /// TypeConverter method override. /// /// ITypeDescriptorContext /// Type to convert to ///true if conversion is possible ///public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if ( destinationType == typeof(InstanceDescriptor) || destinationType == typeof(string)) { return true; } else { return false; } } /// /// ConvertFrom /// ///public override object ConvertFrom( ITypeDescriptorContext td, CultureInfo cultureInfo, object value) { string stringValue = value as string; // Override the converter for sentinel values if (stringValue != null) { stringValue = stringValue.Trim(); if (stringValue == "Automatic") { return Duration.Automatic; } else if (stringValue == "Forever") { return Duration.Forever; } } TimeSpan duration = TimeSpan.Zero; if(_timeSpanConverter == null) { _timeSpanConverter = new TimeSpanConverter(); } duration = (TimeSpan)_timeSpanConverter.ConvertFrom(td, cultureInfo, value); return new Duration(duration); } /// /// TypeConverter method implementation. /// /// ITypeDescriptorContext /// current culture (see CLR specs) /// value to convert from /// Type to convert to ///converted value ////// /// Critical: calls InstanceDescriptor ctor which LinkDemands /// PublicOK: can only make an InstanceDescriptor for Duration, not an arbitrary class /// [SecurityCritical] public override object ConvertTo( ITypeDescriptorContext context, CultureInfo cultureInfo, object value, Type destinationType) { if (destinationType != null && value is Duration) { Duration durationValue = (Duration)value; if (destinationType == typeof(InstanceDescriptor)) { MemberInfo mi; if (durationValue.HasTimeSpan) { mi = typeof(Duration).GetConstructor(new Type[] { typeof(TimeSpan) }); return new InstanceDescriptor(mi, new object[] { durationValue.TimeSpan }); } else if (durationValue == Duration.Forever) { mi = typeof(Duration).GetProperty("Forever"); return new InstanceDescriptor(mi, null); } else { Debug.Assert(durationValue == Duration.Automatic); // Only other legal duration type mi = typeof(Duration).GetProperty("Automatic"); return new InstanceDescriptor(mi, null); } } else if (destinationType == typeof(string)) { return durationValue.ToString(); } } // Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.) return base.ConvertTo(context, cultureInfo, value, destinationType); } private static TimeSpanConverter _timeSpanConverter; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //------------------------------------------------------------------------------ // Microsoft Windows Client Platform // Copyright (c) Microsoft Corporation, 2004 // // File: DurationConverter.cs //----------------------------------------------------------------------------- using System; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Diagnostics; using System.Globalization; using System.Reflection; using System.Security; namespace System.Windows { ////// Provides a type converter to convert Duration to and from other representations. /// public class DurationConverter : TypeConverter { ////// CanConvertFrom - Returns whether or not this class can convert from a given type /// ///public override bool CanConvertFrom(ITypeDescriptorContext td, Type t) { if (t == typeof(string)) { return true; } else { return false; } } /// /// TypeConverter method override. /// /// ITypeDescriptorContext /// Type to convert to ///true if conversion is possible ///public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if ( destinationType == typeof(InstanceDescriptor) || destinationType == typeof(string)) { return true; } else { return false; } } /// /// ConvertFrom /// ///public override object ConvertFrom( ITypeDescriptorContext td, CultureInfo cultureInfo, object value) { string stringValue = value as string; // Override the converter for sentinel values if (stringValue != null) { stringValue = stringValue.Trim(); if (stringValue == "Automatic") { return Duration.Automatic; } else if (stringValue == "Forever") { return Duration.Forever; } } TimeSpan duration = TimeSpan.Zero; if(_timeSpanConverter == null) { _timeSpanConverter = new TimeSpanConverter(); } duration = (TimeSpan)_timeSpanConverter.ConvertFrom(td, cultureInfo, value); return new Duration(duration); } /// /// TypeConverter method implementation. /// /// ITypeDescriptorContext /// current culture (see CLR specs) /// value to convert from /// Type to convert to ///converted value ////// /// Critical: calls InstanceDescriptor ctor which LinkDemands /// PublicOK: can only make an InstanceDescriptor for Duration, not an arbitrary class /// [SecurityCritical] public override object ConvertTo( ITypeDescriptorContext context, CultureInfo cultureInfo, object value, Type destinationType) { if (destinationType != null && value is Duration) { Duration durationValue = (Duration)value; if (destinationType == typeof(InstanceDescriptor)) { MemberInfo mi; if (durationValue.HasTimeSpan) { mi = typeof(Duration).GetConstructor(new Type[] { typeof(TimeSpan) }); return new InstanceDescriptor(mi, new object[] { durationValue.TimeSpan }); } else if (durationValue == Duration.Forever) { mi = typeof(Duration).GetProperty("Forever"); return new InstanceDescriptor(mi, null); } else { Debug.Assert(durationValue == Duration.Automatic); // Only other legal duration type mi = typeof(Duration).GetProperty("Automatic"); return new InstanceDescriptor(mi, null); } } else if (destinationType == typeof(string)) { return durationValue.ToString(); } } // Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.) return base.ConvertTo(context, cultureInfo, value, destinationType); } private static TimeSpanConverter _timeSpanConverter; } } // 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
- DataControlImageButton.cs
- RSAOAEPKeyExchangeDeformatter.cs
- WrappedOptions.cs
- LockRenewalTask.cs
- DataContext.cs
- MaskedTextProvider.cs
- LineBreak.cs
- ExtenderProvidedPropertyAttribute.cs
- WebSysDescriptionAttribute.cs
- StyleBamlRecordReader.cs
- PresentationAppDomainManager.cs
- HtmlTableRowCollection.cs
- Thickness.cs
- LockedAssemblyCache.cs
- XpsInterleavingPolicy.cs
- ServicesUtilities.cs
- SecUtil.cs
- Context.cs
- KoreanLunisolarCalendar.cs
- Attributes.cs
- ExpressionNode.cs
- PauseStoryboard.cs
- ErrorWebPart.cs
- GlyphRun.cs
- IntegrationExceptionEventArgs.cs
- AsymmetricSignatureFormatter.cs
- EnumBuilder.cs
- XamlInterfaces.cs
- Slider.cs
- UriExt.cs
- WebPartDisplayMode.cs
- HttpCapabilitiesEvaluator.cs
- CommandID.cs
- MetadataSerializer.cs
- ZipIOBlockManager.cs
- SafeArrayRankMismatchException.cs
- Bidi.cs
- ButtonFlatAdapter.cs
- RuntimeConfig.cs
- connectionpool.cs
- PointLightBase.cs
- ThousandthOfEmRealPoints.cs
- TextFormatterImp.cs
- RequestChannel.cs
- LeafCellTreeNode.cs
- SQLUtility.cs
- UserNamePasswordClientCredential.cs
- PrePrepareMethodAttribute.cs
- SerialPort.cs
- PathFigure.cs
- PropertyChange.cs
- TextUtf8RawTextWriter.cs
- AlternateViewCollection.cs
- ElementHostPropertyMap.cs
- ControlCollection.cs
- OleDbInfoMessageEvent.cs
- MulticastIPAddressInformationCollection.cs
- WebPartCatalogCloseVerb.cs
- Compress.cs
- DeclaredTypeElement.cs
- DispatcherHooks.cs
- PriorityQueue.cs
- SafeFileMappingHandle.cs
- DocumentOrderComparer.cs
- TreeNodeStyle.cs
- DataErrorValidationRule.cs
- SvcMapFileSerializer.cs
- Decoder.cs
- SimpleMailWebEventProvider.cs
- UInt16Converter.cs
- FileDialog_Vista.cs
- StoreAnnotationsMap.cs
- CustomErrorCollection.cs
- DocComment.cs
- XmlNodeChangedEventArgs.cs
- KeyEvent.cs
- ButtonFlatAdapter.cs
- XmlSerializerNamespaces.cs
- PerformanceCounterLib.cs
- ComplexTypeEmitter.cs
- InfoCardServiceInstallComponent.cs
- PersonalizationState.cs
- EditorAttribute.cs
- CommandManager.cs
- DurableTimerExtension.cs
- BrowserCapabilitiesFactory.cs
- IsolatedStorageFilePermission.cs
- Label.cs
- ipaddressinformationcollection.cs
- Helpers.cs
- ToolStripItemRenderEventArgs.cs
- CapabilitiesAssignment.cs
- ByValueEqualityComparer.cs
- PermissionSetTriple.cs
- Color.cs
- PhysicalFontFamily.cs
- DataRelationCollection.cs
- NetworkInformationException.cs
- SelectionWordBreaker.cs
- RelatedEnd.cs