Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Core / System / Windows / DurationConverter.cs / 2 / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ApplicationProxyInternal.cs
- StorageModelBuildProvider.cs
- EndPoint.cs
- AssemblyResourceLoader.cs
- GeneralTransform3D.cs
- Schema.cs
- DataException.cs
- ServiceContractListItem.cs
- SqlTypeSystemProvider.cs
- MsmqAppDomainProtocolHandler.cs
- CodeArgumentReferenceExpression.cs
- StateRuntime.cs
- ACE.cs
- CodeEventReferenceExpression.cs
- Panel.cs
- Menu.cs
- XmlSchemaInclude.cs
- BuildResult.cs
- X509ChainElement.cs
- SkinBuilder.cs
- ImageMapEventArgs.cs
- DecimalFormatter.cs
- CheckBoxPopupAdapter.cs
- PageBuildProvider.cs
- Logging.cs
- compensatingcollection.cs
- ErrorHandler.cs
- HttpHandler.cs
- unsafeIndexingFilterStream.cs
- CompilerParameters.cs
- PhysicalAddress.cs
- DbConnectionClosed.cs
- TreeNodeCollectionEditor.cs
- BindingEditor.xaml.cs
- AttributeUsageAttribute.cs
- NamedPermissionSet.cs
- DataGridViewImageCell.cs
- ContextBase.cs
- WebRequest.cs
- AssemblyBuilder.cs
- InfoCardCryptoHelper.cs
- Thumb.cs
- DateTimeFormatInfo.cs
- ContractMapping.cs
- TokenBasedSetEnumerator.cs
- RemotingServices.cs
- PenCursorManager.cs
- PTUtility.cs
- ExpressionConverter.cs
- SimpleRecyclingCache.cs
- GetPageNumberCompletedEventArgs.cs
- DeliveryRequirementsAttribute.cs
- SqlInternalConnectionTds.cs
- InheritedPropertyChangedEventArgs.cs
- DifferencingCollection.cs
- WindowsFormsHost.cs
- InkCanvasSelection.cs
- IFlowDocumentViewer.cs
- NgenServicingAttributes.cs
- MsmqProcessProtocolHandler.cs
- bidPrivateBase.cs
- XPathNavigator.cs
- CheckableControlBaseAdapter.cs
- PasswordDeriveBytes.cs
- MappingModelBuildProvider.cs
- ValidationRuleCollection.cs
- NotCondition.cs
- RectangleGeometry.cs
- AutomationProperty.cs
- ListControlDesigner.cs
- __TransparentProxy.cs
- CodeValidator.cs
- ContextDataSourceContextData.cs
- BlurEffect.cs
- DataGridViewAdvancedBorderStyle.cs
- InputReportEventArgs.cs
- OptimizedTemplateContent.cs
- SqlCacheDependency.cs
- SystemResourceKey.cs
- MapPathBasedVirtualPathProvider.cs
- WebRequestModuleElementCollection.cs
- MatrixAnimationUsingKeyFrames.cs
- _CookieModule.cs
- Dynamic.cs
- MeshGeometry3D.cs
- sitestring.cs
- HostUtils.cs
- DbFunctionCommandTree.cs
- ListCollectionView.cs
- TableItemStyle.cs
- PeerNode.cs
- NavigationPropertyEmitter.cs
- StateChangeEvent.cs
- AutomationPatternInfo.cs
- FrameworkPropertyMetadata.cs
- DocumentViewerHelper.cs
- Trace.cs
- ErrorEventArgs.cs
- XmlElementList.cs
- SeverityFilter.cs