Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Core / CSharp / System / Windows / DurationConverter.cs / 1305600 / 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
- ImmutableClientRuntime.cs
- DurationConverter.cs
- ManualResetEvent.cs
- TaskSchedulerException.cs
- FrameworkContentElement.cs
- Graph.cs
- CodeStatementCollection.cs
- ReservationNotFoundException.cs
- CodeAttachEventStatement.cs
- SettingsPropertyNotFoundException.cs
- DtcInterfaces.cs
- ProxyHwnd.cs
- FormatterServicesNoSerializableCheck.cs
- InfoCardSymmetricCrypto.cs
- HeaderCollection.cs
- FixedHighlight.cs
- DrawingContextWalker.cs
- NCryptNative.cs
- UIElementHelper.cs
- ProcessHostFactoryHelper.cs
- SystemNetHelpers.cs
- EntityContainerEntitySetDefiningQuery.cs
- StorageMappingFragment.cs
- NonVisualControlAttribute.cs
- SimpleRecyclingCache.cs
- TableLayoutCellPaintEventArgs.cs
- Types.cs
- RepeaterItem.cs
- TypeToken.cs
- HtmlShim.cs
- BindingContext.cs
- GenerateDerivedKeyRequest.cs
- MailBnfHelper.cs
- DependencyObjectProvider.cs
- brushes.cs
- BitmapDecoder.cs
- XmlSchemaProviderAttribute.cs
- Point3DValueSerializer.cs
- FreeFormDesigner.cs
- IconHelper.cs
- UrlMappingCollection.cs
- BindingExpression.cs
- UpdatePanelTriggerCollection.cs
- Stroke2.cs
- MimeParameterWriter.cs
- DataGridLinkButton.cs
- PropertyChangeTracker.cs
- MetafileHeader.cs
- SortedDictionary.cs
- CapabilitiesPattern.cs
- RestClientProxyHandler.cs
- WorkflowEventArgs.cs
- documentation.cs
- ExecutionEngineException.cs
- DbProviderFactoriesConfigurationHandler.cs
- IdentitySection.cs
- RenderData.cs
- ProcessThreadDesigner.cs
- ThicknessAnimation.cs
- Binding.cs
- ConfigWriter.cs
- HttpListener.cs
- TextTabProperties.cs
- SQLMoney.cs
- SafeBitVector32.cs
- BinaryMessageFormatter.cs
- DataGridViewCellFormattingEventArgs.cs
- AuthenticationService.cs
- WebControl.cs
- FixUpCollection.cs
- EntityContainer.cs
- GridViewUpdatedEventArgs.cs
- ResolveMatchesApril2005.cs
- SmtpTransport.cs
- WebMessageEncodingElement.cs
- RoutingUtilities.cs
- ActivityExecutorSurrogate.cs
- ConfigPathUtility.cs
- PathFigure.cs
- MethodAccessException.cs
- DeflateEmulationStream.cs
- EntityUtil.cs
- DBConnection.cs
- NoResizeHandleGlyph.cs
- TextRunProperties.cs
- HostedElements.cs
- ArrangedElementCollection.cs
- StorageComplexTypeMapping.cs
- RSAProtectedConfigurationProvider.cs
- CapabilitiesState.cs
- FixedPage.cs
- DoubleAnimationUsingKeyFrames.cs
- TextAction.cs
- ShaderRenderModeValidation.cs
- AvTraceDetails.cs
- GeometryConverter.cs
- ExtensionSimplifierMarkupObject.cs
- TextEditorThreadLocalStore.cs
- CodeTypeParameter.cs
- BindStream.cs