Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / XamlIntegration / TypeConverterBase.cs / 1305376 / TypeConverterBase.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Activities.XamlIntegration { using System; using System.Collections.Concurrent; using System.ComponentModel; using System.Globalization; using System.Runtime; using System.Threading; using System.Xaml; public abstract class TypeConverterBase : TypeConverter { Lazy> helpers = new Lazy >(); TypeConverterHelper helper; Type baseType; Type helperType; internal TypeConverterBase(Type baseType, Type helperType) { this.baseType = baseType; this.helperType = helperType; } internal TypeConverterBase(Type targetType, Type baseType, Type helperType) { this.helper = GetTypeConverterHelper(targetType, baseType, helperType); } public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == TypeHelper.StringType) { return true; } return base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == TypeHelper.StringType) { return false; } return base.CanConvertTo(context, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string stringValue = value as string; if (stringValue != null) { TypeConverterHelper currentHelper = helper; if (currentHelper == null) { IDestinationTypeProvider targetService = context.GetService(typeof(IDestinationTypeProvider)) as IDestinationTypeProvider; Type targetType = targetService.GetDestinationType(); if (!this.helpers.Value.TryGetValue(targetType, out currentHelper)) { currentHelper = GetTypeConverterHelper(targetType, this.baseType, this.helperType); if (!this.helpers.Value.TryAdd(targetType, currentHelper)) { if (!this.helpers.Value.TryGetValue(targetType, out currentHelper)) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.TypeConverterHelperCacheAddFailed(targetType))); } } } } object result = currentHelper.UntypedConvertFromString(stringValue, context); return result; } return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { return base.ConvertTo(context, culture, value, destinationType); } TypeConverterHelper GetTypeConverterHelper(Type targetType, Type baseType, Type helperType) { Type[] genericTypeArguments; if (baseType.BaseType == targetType) { // support non-generic ActivityWithResult, In/Out/InOutArgument genericTypeArguments = new Type[] { TypeHelper.ObjectType }; } else { // Find baseType in the base class list of targetType while (!targetType.IsGenericType || !(targetType.GetGenericTypeDefinition() == baseType)) { if (targetType == TypeHelper.ObjectType) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.InvalidTypeConverterUsage)); } targetType = targetType.BaseType; } genericTypeArguments = targetType.GetGenericArguments(); } Type concreteHelperType = helperType.MakeGenericType(genericTypeArguments); return (TypeConverterHelper)Activator.CreateInstance(concreteHelperType); } internal abstract class TypeConverterHelper { public abstract object UntypedConvertFromString(string text, ITypeDescriptorContext context); public static T GetService (ITypeDescriptorContext context) where T : class { T service = (T)context.GetService(typeof(T)); if (service == null) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.InvalidTypeConverterUsage)); } return service; } } internal abstract class TypeConverterHelper : TypeConverterHelper { public abstract T ConvertFromString(string text, ITypeDescriptorContext context); public sealed override object UntypedConvertFromString(string text, ITypeDescriptorContext context) { return ConvertFromString(text, context); } } } } // 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
- ApplicationSecurityInfo.cs
- RegularExpressionValidator.cs
- Html32TextWriter.cs
- ParameterSubsegment.cs
- ViewManager.cs
- _ProxyChain.cs
- URLAttribute.cs
- XmlSerializerVersionAttribute.cs
- XsltLoader.cs
- XmlNodeComparer.cs
- ReferencedAssemblyResolver.cs
- ListItemCollection.cs
- ImageFormat.cs
- FileDialog.cs
- Rijndael.cs
- HuffCodec.cs
- ServerValidateEventArgs.cs
- Exception.cs
- SettingsProviderCollection.cs
- UTF8Encoding.cs
- TextSelection.cs
- IApplicationTrustManager.cs
- SourceFilter.cs
- EmptyReadOnlyDictionaryInternal.cs
- PriorityRange.cs
- ACE.cs
- HyperLinkColumn.cs
- ObjectStateEntryDbUpdatableDataRecord.cs
- SerialReceived.cs
- DataGridViewControlCollection.cs
- Point3DValueSerializer.cs
- NaturalLanguageHyphenator.cs
- MsmqIntegrationSecurityMode.cs
- ConnectionPointCookie.cs
- SqlNodeAnnotations.cs
- DataGridViewCellMouseEventArgs.cs
- TextStore.cs
- AsymmetricKeyExchangeFormatter.cs
- Freezable.cs
- Stack.cs
- Boolean.cs
- DataSourceSelectArguments.cs
- ViewStateAttachedPropertyFeature.cs
- ProfileSection.cs
- SqlConnectionString.cs
- XmlReflectionMember.cs
- LogFlushAsyncResult.cs
- FtpCachePolicyElement.cs
- ProfileGroupSettingsCollection.cs
- FormParameter.cs
- SqlBulkCopyColumnMapping.cs
- DemultiplexingClientMessageFormatter.cs
- WebPartHelpVerb.cs
- WindowsListBox.cs
- StorageMappingItemCollection.cs
- FileDialog.cs
- NodeLabelEditEvent.cs
- relpropertyhelper.cs
- TabControlEvent.cs
- ProgressiveCrcCalculatingStream.cs
- EvidenceBase.cs
- XPathAncestorQuery.cs
- DataPagerFieldCommandEventArgs.cs
- MethodBuilder.cs
- SqlDataSourceFilteringEventArgs.cs
- ListBindingConverter.cs
- DataGridViewComboBoxColumn.cs
- ListenerSingletonConnectionReader.cs
- SBCSCodePageEncoding.cs
- EventListenerClientSide.cs
- OrderByQueryOptionExpression.cs
- CollectionChangeEventArgs.cs
- DispatcherTimer.cs
- webeventbuffer.cs
- ContainerUtilities.cs
- BitArray.cs
- UrlAuthFailedErrorFormatter.cs
- PointLightBase.cs
- MetadataProperty.cs
- SqlClientWrapperSmiStream.cs
- TextCompositionEventArgs.cs
- XXXInfos.cs
- XPathNodeHelper.cs
- ActivityBindForm.Designer.cs
- TdsEnums.cs
- WizardSideBarListControlItem.cs
- HashMembershipCondition.cs
- ResourceManagerWrapper.cs
- ResourceReader.cs
- DialogResultConverter.cs
- TableSectionStyle.cs
- Panel.cs
- FullTrustAssembly.cs
- AsmxEndpointPickerExtension.cs
- XsltArgumentList.cs
- _TransmitFileOverlappedAsyncResult.cs
- QuaternionAnimation.cs
- GetReadStreamResult.cs
- DynamicPropertyReader.cs
- TextCompositionManager.cs