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
- Visual3D.cs
- EdmConstants.cs
- ServerValidateEventArgs.cs
- HtmlSelect.cs
- SynchronizedDispatch.cs
- ConfigPathUtility.cs
- brushes.cs
- TransportDefaults.cs
- ObjectStateFormatter.cs
- XmlMemberMapping.cs
- SystemResourceKey.cs
- CodePrimitiveExpression.cs
- HtmlValidationSummaryAdapter.cs
- InkPresenterAutomationPeer.cs
- DrawItemEvent.cs
- PageAdapter.cs
- DataGridViewAdvancedBorderStyle.cs
- FilePrompt.cs
- CannotUnloadAppDomainException.cs
- ObjectViewFactory.cs
- TagMapCollection.cs
- AsyncMethodInvoker.cs
- MinMaxParagraphWidth.cs
- XmlRawWriterWrapper.cs
- ShutDownListener.cs
- DispatcherObject.cs
- Point3DValueSerializer.cs
- CultureMapper.cs
- AdornerLayer.cs
- EndPoint.cs
- ObjectDataProvider.cs
- EmptyReadOnlyDictionaryInternal.cs
- ReferentialConstraint.cs
- DnsEndPoint.cs
- EntitySqlQueryCacheKey.cs
- XmlNamespaceDeclarationsAttribute.cs
- BamlLocalizableResourceKey.cs
- ProfileSection.cs
- ConfigXmlComment.cs
- Literal.cs
- EventSetterHandlerConverter.cs
- ConnectionConsumerAttribute.cs
- RangeContentEnumerator.cs
- FileUtil.cs
- DbException.cs
- XmlUtil.cs
- ConfigXmlCDataSection.cs
- FileResponseElement.cs
- BrowserInteropHelper.cs
- InheritanceContextHelper.cs
- SecurityCookieModeValidator.cs
- FilteredAttributeCollection.cs
- DeobfuscatingStream.cs
- DataService.cs
- CqlBlock.cs
- FileEnumerator.cs
- __Error.cs
- AssemblyBuilder.cs
- DictionaryBase.cs
- DesignerDataView.cs
- NavigationPropertyEmitter.cs
- SessionEndedEventArgs.cs
- Profiler.cs
- DbDataSourceEnumerator.cs
- TextParagraphView.cs
- datacache.cs
- DataTrigger.cs
- MenuItem.cs
- CompilationSection.cs
- UserControlCodeDomTreeGenerator.cs
- Classification.cs
- GcSettings.cs
- StaticExtension.cs
- IndexObject.cs
- AnimationClock.cs
- SiteMapProvider.cs
- TrustLevel.cs
- ExpressionList.cs
- SplashScreenNativeMethods.cs
- BoundPropertyEntry.cs
- SamlAuthorizationDecisionStatement.cs
- cryptoapiTransform.cs
- RolePrincipal.cs
- ByeOperation11AsyncResult.cs
- ObjectAssociationEndMapping.cs
- EndpointDesigner.cs
- TdsEnums.cs
- SiteMapNodeCollection.cs
- FrugalList.cs
- RectKeyFrameCollection.cs
- XmlSecureResolver.cs
- EventRecordWrittenEventArgs.cs
- AppDomainUnloadedException.cs
- MaterialGroup.cs
- Rect.cs
- BigInt.cs
- BooleanKeyFrameCollection.cs
- LocatorPart.cs
- XmlEventCache.cs
- HttpStreamXmlDictionaryWriter.cs