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)
{
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.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- BuilderPropertyEntry.cs
- ComponentManagerBroker.cs
- TransactionBridgeSection.cs
- ProfessionalColorTable.cs
- AppDomain.cs
- ComponentManagerBroker.cs
- XmlSiteMapProvider.cs
- _WebProxyDataBuilder.cs
- SvcMapFileLoader.cs
- Parsers.cs
- SecurityTokenSerializer.cs
- PlainXmlWriter.cs
- DataGridViewSelectedRowCollection.cs
- Sql8ConformanceChecker.cs
- AnimationException.cs
- Interlocked.cs
- KeyedCollection.cs
- TableItemStyle.cs
- Task.cs
- IOException.cs
- Base64Stream.cs
- DataGridViewControlCollection.cs
- WindowsListViewItemStartMenu.cs
- XamlSerializerUtil.cs
- XmlIgnoreAttribute.cs
- BulletedListEventArgs.cs
- BreakRecordTable.cs
- Soap.cs
- ModuleElement.cs
- LineVisual.cs
- ExtensionFile.cs
- Base64Decoder.cs
- VirtualDirectoryMappingCollection.cs
- DeliveryRequirementsAttribute.cs
- InternalDispatchObject.cs
- DataGridColumnHeadersPresenter.cs
- OutArgument.cs
- DataControlButton.cs
- ExtensionQuery.cs
- CodeDirectoryCompiler.cs
- RecordsAffectedEventArgs.cs
- ListViewItemCollectionEditor.cs
- ExpandableObjectConverter.cs
- ContentOperations.cs
- xsdvalidator.cs
- DataGridItemCollection.cs
- FixedNode.cs
- RedBlackList.cs
- ControlBindingsConverter.cs
- DataGridViewCellCancelEventArgs.cs
- ProxyHelper.cs
- BitmapEffectGeneralTransform.cs
- TdsParser.cs
- TextTabProperties.cs
- DeviceFiltersSection.cs
- Activator.cs
- Substitution.cs
- RMEnrollmentPage3.cs
- IMembershipProvider.cs
- TextWriter.cs
- Queue.cs
- PropertiesTab.cs
- X509AsymmetricSecurityKey.cs
- Faults.cs
- XhtmlBasicPageAdapter.cs
- Qualifier.cs
- DeploymentSection.cs
- ComponentResourceManager.cs
- ComponentManagerBroker.cs
- UnsafeNativeMethods.cs
- DataGridColumnHeadersPresenter.cs
- WindowAutomationPeer.cs
- MobileErrorInfo.cs
- ToolStripItemClickedEventArgs.cs
- ResXDataNode.cs
- ProxyWebPartConnectionCollection.cs
- WebBrowserSiteBase.cs
- LocalizationComments.cs
- BamlLocalizerErrorNotifyEventArgs.cs
- TypePresenter.xaml.cs
- XmlSchemaDatatype.cs
- ListControlDataBindingHandler.cs
- ProtocolsConfigurationEntry.cs
- HttpResponseInternalWrapper.cs
- COM2TypeInfoProcessor.cs
- DbgCompiler.cs
- XmlQueryOutput.cs
- XPathAncestorIterator.cs
- LocalizationComments.cs
- CompensatableTransactionScopeActivityDesigner.cs
- IdentityNotMappedException.cs
- ConfigXmlComment.cs
- X509AsymmetricSecurityKey.cs
- Activity.cs
- Empty.cs
- SQLBytes.cs
- LiteralDesigner.cs
- RunClient.cs
- Rotation3D.cs
- TrackingStringDictionary.cs