Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / XamlIntegration / ActivityWithResultConverter.cs / 1305376 / ActivityWithResultConverter.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Activities.XamlIntegration { using Microsoft.VisualBasic.Activities; using System; using System.Activities.Expressions; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Runtime; using System.Text.RegularExpressions; using System.Windows.Markup; using System.Xaml; using System.Xaml.Schema; using System.Xml.Linq; using Microsoft.VisualBasic.Activities.XamlIntegration; public sealed class ActivityWithResultConverter : TypeConverterBase { public ActivityWithResultConverter() : base(typeof(Activity<>), typeof(ExpressionConverterHelper<>)) { } public ActivityWithResultConverter(Type type) : base(type, typeof(Activity<>), typeof(ExpressionConverterHelper<>)) { } internal static object GetRootTemplatedActivity(IServiceProvider serviceProvider) { // For now, we only support references to the root Activity when we're inside an Activity.Body // Note that in the case of nested activity bodies, this gives us the outer activity IRootObjectProvider rootProvider = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider; if (rootProvider == null) { return null; } IAmbientProvider ambientProvider = serviceProvider.GetService(typeof(IAmbientProvider)) as IAmbientProvider; if (ambientProvider == null) { return null; } IXamlSchemaContextProvider schemaContextProvider = serviceProvider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider; if (schemaContextProvider == null) { return null; } XamlMember activityBody = GetXamlMember(schemaContextProvider.SchemaContext, typeof(Activity), "Implementation"); XamlMember dynamicActivityBody = GetXamlMember(schemaContextProvider.SchemaContext, typeof(DynamicActivity), "Implementation"); if (activityBody == null || dynamicActivityBody == null) { return null; } if (ambientProvider.GetFirstAmbientValue(null, activityBody, dynamicActivityBody) == null) { return null; } object rootActivity = rootProvider.RootObject as Activity; return rootActivity; } static XamlMember GetXamlMember(XamlSchemaContext schemaContext, Type type, string memberName) { XamlType xamlType = schemaContext.GetXamlType(type); if (xamlType == null) { return null; } XamlMember xamlMember = xamlType.GetMember(memberName); return xamlMember; } internal sealed class ExpressionConverterHelper: TypeConverterHelper > { static Regex LiteralEscapeRegex = new Regex(@"^(%+\[)"); static Type LocationHelperType = typeof(LocationHelper<>); TypeConverter baseConverter; Type valueType; LocationHelper locationHelper; // true if we're dealing with a Location public ExpressionConverterHelper() : this(TypeHelper.AreTypesCompatible(typeof(T), typeof(Location))) { } public ExpressionConverterHelper(bool isLocationType) { this.valueType = typeof(T); if (isLocationType) { Fx.Assert(this.valueType.IsGenericType && this.valueType.GetGenericArguments().Length == 1, "Should only get Location here"); this.valueType = this.valueType.GetGenericArguments()[0]; Type concreteHelperType = LocationHelperType.MakeGenericType(typeof(T), this.valueType); this.locationHelper = (LocationHelper)Activator.CreateInstance(concreteHelperType); } } TypeConverter BaseConverter { get { if (this.baseConverter == null) { this.baseConverter = TypeDescriptor.GetConverter(this.valueType); } return this.baseConverter; } } public override Activity ConvertFromString(string text, ITypeDescriptorContext context) { if (IsExpression(text)) { // Expression. Use the expression parser. string expressionText = text.Substring(1, text.Length - 2); if (this.locationHelper != null) { // return (Activity )this.locationHelper.CreateExpression(expressionText); } else { // return new VisualBasicValue () { ExpressionText = expressionText }; } } else { if (this.locationHelper != null) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.InvalidLocationExpression)); } // look for "%[....]" escape pattern if (text.EndsWith("]", StringComparison.Ordinal) && LiteralEscapeRegex.IsMatch(text)) { // strip off the very front-most '%' from the original string text = text.Substring(1, text.Length - 1); } T literalValue; if (text is T) { literalValue = (T)(object)text; } else if (text == string.Empty) // workaround for System.Runtime.Xaml bug { literalValue = default(T); } else { // Literal value. Invoke the base type converter. literalValue = (T)BaseConverter.ConvertFromString(context, text); } return new Literal { Value = literalValue }; } } static bool IsExpression(string text) { return (text.StartsWith("[", StringComparison.Ordinal) && text.EndsWith("]", StringComparison.Ordinal)); } // to perform the generics dance around Locations we need these helpers abstract class LocationHelper { public abstract Activity CreateExpression(string expressionText); } class LocationHelper : LocationHelper { public override Activity CreateExpression(string expressionText) { return new VisualBasicReference () { ExpressionText = expressionText }; } } } } } // 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
- SingleConverter.cs
- ConnectorSelectionGlyph.cs
- RankException.cs
- StructuredTypeInfo.cs
- Assembly.cs
- Trace.cs
- DPCustomTypeDescriptor.cs
- XmlSchemaAnnotated.cs
- WebServicesDescriptionAttribute.cs
- CodeComment.cs
- TransformerInfo.cs
- CharConverter.cs
- UInt32Storage.cs
- RelationshipEnd.cs
- PropertyPath.cs
- AssemblyHash.cs
- EntryIndex.cs
- XamlUtilities.cs
- PackageDigitalSignatureManager.cs
- PlatformCulture.cs
- HttpCachePolicyElement.cs
- ObsoleteAttribute.cs
- OpenTypeLayoutCache.cs
- Message.cs
- IODescriptionAttribute.cs
- NativeMethods.cs
- SqlClientWrapperSmiStream.cs
- Renderer.cs
- DnsElement.cs
- SmtpNegotiateAuthenticationModule.cs
- SqlDataSourceFilteringEventArgs.cs
- SmiEventSink.cs
- DropShadowBitmapEffect.cs
- XmlSyndicationContent.cs
- AdapterUtil.cs
- SelectorItemAutomationPeer.cs
- WindowsAltTab.cs
- MemberDomainMap.cs
- TriggerAction.cs
- DataGridItem.cs
- LinqDataSourceEditData.cs
- InfoCardRSAPKCS1KeyExchangeDeformatter.cs
- TraceSection.cs
- GridViewRowCollection.cs
- SecurityHelper.cs
- keycontainerpermission.cs
- BooleanConverter.cs
- CoTaskMemHandle.cs
- BaseTemplateParser.cs
- IndexedGlyphRun.cs
- RenderDataDrawingContext.cs
- CompoundFileStreamReference.cs
- SystemThemeKey.cs
- RecommendedAsConfigurableAttribute.cs
- ScrollBarRenderer.cs
- EmbeddedMailObject.cs
- ConnectionStringsSection.cs
- GridViewRowCollection.cs
- SqlParameter.cs
- MultipleViewPatternIdentifiers.cs
- XmlNodeChangedEventArgs.cs
- TextStore.cs
- TabPageDesigner.cs
- BooleanFacetDescriptionElement.cs
- TableCellsCollectionEditor.cs
- GlyphsSerializer.cs
- TemplateModeChangedEventArgs.cs
- SmiConnection.cs
- ItemAutomationPeer.cs
- TagMapInfo.cs
- TrailingSpaceComparer.cs
- ReadOnlyHierarchicalDataSource.cs
- ProfileParameter.cs
- _TransmitFileOverlappedAsyncResult.cs
- MarginCollapsingState.cs
- PaintValueEventArgs.cs
- FixedSOMPage.cs
- ExpressionsCollectionEditor.cs
- GridViewRow.cs
- TextElementCollection.cs
- WebPartDescriptionCollection.cs
- Utilities.cs
- ContentPresenter.cs
- CultureSpecificStringDictionary.cs
- Pkcs7Recipient.cs
- DmlSqlGenerator.cs
- HyperLinkColumn.cs
- ClientConvert.cs
- AttributeProviderAttribute.cs
- DataGridViewCheckBoxColumn.cs
- QilPatternFactory.cs
- ReferenceService.cs
- ControlBindingsConverter.cs
- OleDbConnection.cs
- RSAPKCS1SignatureDeformatter.cs
- TemplateControl.cs
- EmbeddedMailObjectsCollection.cs
- WindowsImpersonationContext.cs
- DetailsViewPagerRow.cs
- SynchronousChannel.cs