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
- StringPropertyBuilder.cs
- SrgsRuleRef.cs
- DeferredReference.cs
- StringFunctions.cs
- SingleAnimation.cs
- PartialArray.cs
- CustomErrorsSectionWrapper.cs
- ProfileService.cs
- MSG.cs
- Zone.cs
- EllipseGeometry.cs
- DPTypeDescriptorContext.cs
- SynchronizationLockException.cs
- BatchStream.cs
- OdbcConnectionString.cs
- safemediahandle.cs
- SerialErrors.cs
- DataSourceSelectArguments.cs
- ExceptionList.cs
- CodeTypeReference.cs
- MILUtilities.cs
- FontInfo.cs
- Query.cs
- SevenBitStream.cs
- SectionXmlInfo.cs
- WebPartExportVerb.cs
- Attributes.cs
- PenThread.cs
- XmlSchemaAppInfo.cs
- BitmapEffect.cs
- Message.cs
- CompoundFileStorageReference.cs
- HyperLinkStyle.cs
- UInt16.cs
- FlagsAttribute.cs
- SHA384.cs
- Int32Rect.cs
- EdgeModeValidation.cs
- Buffer.cs
- AttachmentCollection.cs
- Variant.cs
- MemberCollection.cs
- RenderDataDrawingContext.cs
- AspNetRouteServiceHttpHandler.cs
- contentDescriptor.cs
- InstanceOwner.cs
- ObjectConverter.cs
- HttpCapabilitiesEvaluator.cs
- TypographyProperties.cs
- BooleanAnimationBase.cs
- WebDescriptionAttribute.cs
- ConnectionManagementElement.cs
- SigningCredentials.cs
- ReadOnlyPropertyMetadata.cs
- KeyValueSerializer.cs
- Semaphore.cs
- SafeProcessHandle.cs
- Serializer.cs
- SoapSchemaImporter.cs
- MsmqBindingMonitor.cs
- DesignBindingPropertyDescriptor.cs
- InvalidateEvent.cs
- SpotLight.cs
- sqlstateclientmanager.cs
- ButtonFlatAdapter.cs
- RectangleF.cs
- PagesChangedEventArgs.cs
- ScriptRegistrationManager.cs
- TempFiles.cs
- ResetableIterator.cs
- PaperSize.cs
- SmtpNtlmAuthenticationModule.cs
- DesignerActionList.cs
- SQLSingleStorage.cs
- CheckBoxFlatAdapter.cs
- SHA256CryptoServiceProvider.cs
- NotSupportedException.cs
- mda.cs
- FileSystemWatcher.cs
- Helper.cs
- FastPropertyAccessor.cs
- WorkflowMarkupSerializationException.cs
- CompilerInfo.cs
- QualifiedCellIdBoolean.cs
- DebugTraceHelper.cs
- SoapAttributeAttribute.cs
- PeerObject.cs
- ByteAnimationUsingKeyFrames.cs
- ItemCheckEvent.cs
- ProcessThread.cs
- Sorting.cs
- PingReply.cs
- Executor.cs
- RsaSecurityToken.cs
- objectresult_tresulttype.cs
- Regex.cs
- MasterPageBuildProvider.cs
- IIS7WorkerRequest.cs
- SocketStream.cs
- ListViewItemSelectionChangedEvent.cs