Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Presentation / System / Activities / Presentation / Model / GenericArgumentsUpdater.cs / 1305376 / GenericArgumentsUpdater.cs
//---------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------- namespace System.Activities.Presentation.Model { using System; using System.Activities; using System.Diagnostics; using System.Linq; using System.Runtime; using System.Activities.Presentation.View; using System.Windows.Threading; class GenericArgumentUpdater { EditingContext context; const string TypeArgumentPropertyName = "TypeArgument"; public GenericArgumentUpdater(EditingContext context) { this.context = context; } public void AddSupportForUpdatingTypeArgument(Type modelItemType) { AttachedPropertytypeArgumentProperty = new AttachedProperty { Name = TypeArgumentPropertyName, OwnerType = modelItemType, Getter = (modelItem) => GetTypeArgument(modelItem), Setter = (modelItem, value) => UpdateTypeArgument(modelItem, value), IsBrowsable = true }; this.context.Services.GetService ().AddProperty(typeArgumentProperty); } private static void UpdateTypeArgument(ModelItem modelItem, Type value) { Type oldModelItemType = modelItem.ItemType; Fx.Assert(oldModelItemType.GetGenericArguments().Count() == 1, "we only support changing a single type parameter ?"); Type newModelItemType = oldModelItemType.GetGenericTypeDefinition().MakeGenericType(value); Fx.Assert(newModelItemType != null, "New model item type needs to be non null or we cannot proceed further"); ModelItem newModelItem = ModelFactory.CreateItem(modelItem.GetEditingContext(), Activator.CreateInstance(newModelItemType)); MorphHelper.MorphObject(modelItem, newModelItem); MorphHelper.MorphProperties(modelItem, newModelItem); if (oldModelItemType.IsSubclassOf(typeof(Activity)) && newModelItemType.IsSubclassOf(typeof(Activity))) { if (string.Equals((string)modelItem.Properties["DisplayName"].ComputedValue, GetActivityDefaultName(oldModelItemType), StringComparison.Ordinal)) { newModelItem.Properties["DisplayName"].SetValue(GetActivityDefaultName(newModelItemType)); } } DesignerView designerView = modelItem.GetEditingContext().Services.GetService (); if (designerView != null) { Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, (Action)(() => { if (designerView.RootDesigner != null && ((WorkflowViewElement)designerView.RootDesigner).ModelItem == modelItem) { designerView.MakeRootDesigner(newModelItem, true); } Selection.SelectOnly(modelItem.GetEditingContext(), newModelItem); })); } } private static Type GetTypeArgument(ModelItem modelItem) { Fx.Assert(modelItem.ItemType.GetGenericArguments().Count() == 1, "we only support changing a single type parameter ?"); return modelItem.ItemType.GetGenericArguments()[0]; } private static string GetActivityDefaultName(Type activityType) { Fx.Assert(activityType.IsSubclassOf(typeof(Activity)), "activityType is not a subclass of System.Activities.Activity"); Activity activity = (Activity)Activator.CreateInstance(activityType); return activity.DisplayName; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------- namespace System.Activities.Presentation.Model { using System; using System.Activities; using System.Diagnostics; using System.Linq; using System.Runtime; using System.Activities.Presentation.View; using System.Windows.Threading; class GenericArgumentUpdater { EditingContext context; const string TypeArgumentPropertyName = "TypeArgument"; public GenericArgumentUpdater(EditingContext context) { this.context = context; } public void AddSupportForUpdatingTypeArgument(Type modelItemType) { AttachedProperty typeArgumentProperty = new AttachedProperty { Name = TypeArgumentPropertyName, OwnerType = modelItemType, Getter = (modelItem) => GetTypeArgument(modelItem), Setter = (modelItem, value) => UpdateTypeArgument(modelItem, value), IsBrowsable = true }; this.context.Services.GetService ().AddProperty(typeArgumentProperty); } private static void UpdateTypeArgument(ModelItem modelItem, Type value) { Type oldModelItemType = modelItem.ItemType; Fx.Assert(oldModelItemType.GetGenericArguments().Count() == 1, "we only support changing a single type parameter ?"); Type newModelItemType = oldModelItemType.GetGenericTypeDefinition().MakeGenericType(value); Fx.Assert(newModelItemType != null, "New model item type needs to be non null or we cannot proceed further"); ModelItem newModelItem = ModelFactory.CreateItem(modelItem.GetEditingContext(), Activator.CreateInstance(newModelItemType)); MorphHelper.MorphObject(modelItem, newModelItem); MorphHelper.MorphProperties(modelItem, newModelItem); if (oldModelItemType.IsSubclassOf(typeof(Activity)) && newModelItemType.IsSubclassOf(typeof(Activity))) { if (string.Equals((string)modelItem.Properties["DisplayName"].ComputedValue, GetActivityDefaultName(oldModelItemType), StringComparison.Ordinal)) { newModelItem.Properties["DisplayName"].SetValue(GetActivityDefaultName(newModelItemType)); } } DesignerView designerView = modelItem.GetEditingContext().Services.GetService (); if (designerView != null) { Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, (Action)(() => { if (designerView.RootDesigner != null && ((WorkflowViewElement)designerView.RootDesigner).ModelItem == modelItem) { designerView.MakeRootDesigner(newModelItem, true); } Selection.SelectOnly(modelItem.GetEditingContext(), newModelItem); })); } } private static Type GetTypeArgument(ModelItem modelItem) { Fx.Assert(modelItem.ItemType.GetGenericArguments().Count() == 1, "we only support changing a single type parameter ?"); return modelItem.ItemType.GetGenericArguments()[0]; } private static string GetActivityDefaultName(Type activityType) { Fx.Assert(activityType.IsSubclassOf(typeof(Activity)), "activityType is not a subclass of System.Activities.Activity"); Activity activity = (Activity)Activator.CreateInstance(activityType); return activity.DisplayName; } } } // 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
- IntegerFacetDescriptionElement.cs
- TimeoutHelper.cs
- EncryptedXml.cs
- PasswordBox.cs
- AxisAngleRotation3D.cs
- IsolatedStorage.cs
- TransformerConfigurationWizardBase.cs
- MimeTypeAttribute.cs
- StrokeCollectionDefaultValueFactory.cs
- PersonalizationStateQuery.cs
- DataGridLinkButton.cs
- Parser.cs
- FontStyles.cs
- WebZoneDesigner.cs
- MachineKeyValidationConverter.cs
- XmlCustomFormatter.cs
- DesignerOptionService.cs
- InvalidFilterCriteriaException.cs
- HtmlForm.cs
- Simplifier.cs
- CryptoConfig.cs
- ComPlusSynchronizationContext.cs
- WebResourceUtil.cs
- ItemAutomationPeer.cs
- FormViewDeleteEventArgs.cs
- EditorResources.cs
- ConfigurationErrorsException.cs
- BitmapEffectInput.cs
- BadImageFormatException.cs
- JumpList.cs
- CommandPlan.cs
- ClientSideQueueItem.cs
- MaterialGroup.cs
- localization.cs
- ServiceOperationInvoker.cs
- Package.cs
- URL.cs
- ZoneButton.cs
- ResXFileRef.cs
- ScrollBar.cs
- ZoneLinkButton.cs
- XmlExtensionFunction.cs
- TrimSurroundingWhitespaceAttribute.cs
- ChangeInterceptorAttribute.cs
- XmlFormatExtensionPrefixAttribute.cs
- DataGridViewRowCollection.cs
- DataGridTemplateColumn.cs
- XmlSchemaSet.cs
- VariableElement.cs
- SchemaImporterExtensionElementCollection.cs
- ListBase.cs
- Number.cs
- WebConfigurationManager.cs
- WebPartRestoreVerb.cs
- FrameworkElement.cs
- TraceEventCache.cs
- ErrorInfoXmlDocument.cs
- DriveNotFoundException.cs
- PreloadedPackages.cs
- SpeakCompletedEventArgs.cs
- SqlComparer.cs
- ProfessionalColorTable.cs
- TraceContextEventArgs.cs
- SqlLiftIndependentRowExpressions.cs
- BmpBitmapEncoder.cs
- ManagementOperationWatcher.cs
- SetIndexBinder.cs
- InvalidCommandTreeException.cs
- SQLInt64Storage.cs
- OutKeywords.cs
- NativeRightsManagementAPIsStructures.cs
- ClientRoleProvider.cs
- DataGridViewCellEventArgs.cs
- GacUtil.cs
- HttpHeaderCollection.cs
- WindowsAuthenticationEventArgs.cs
- Activity.cs
- LogExtentCollection.cs
- MyContact.cs
- MinimizableAttributeTypeConverter.cs
- Identity.cs
- AudienceUriMode.cs
- PublisherIdentityPermission.cs
- SetterBaseCollection.cs
- Brush.cs
- VectorCollectionConverter.cs
- RectAnimationClockResource.cs
- WebControl.cs
- JsonXmlDataContract.cs
- ToolTip.cs
- ObjectSet.cs
- MissingSatelliteAssemblyException.cs
- BindingGraph.cs
- XNameConverter.cs
- FilterInvalidBodyAccessException.cs
- AccessedThroughPropertyAttribute.cs
- HttpHeaderCollection.cs
- SmtpSection.cs
- FrameworkElement.cs
- MembershipSection.cs