Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WF / Common / AuthoringOM / Serializer / DependencyObjectCodeDomSerializer.cs / 1305376 / DependencyObjectCodeDomSerializer.cs
namespace System.Workflow.ComponentModel.Serialization { using System; using System.CodeDom; using System.ComponentModel; using System.ComponentModel.Design; using System.ComponentModel.Design.Serialization; using System.Collections; using System.Resources; using System.Workflow.ComponentModel.Design; using System.Collections.Generic; using Microsoft.CSharp; using System.Workflow.ComponentModel; using System.Workflow.ComponentModel.Compiler; using System.CodeDom.Compiler; using System.IO; using System.Reflection; using System.Diagnostics; #region Class DependencyObjectCodeDomSerializer public class DependencyObjectCodeDomSerializer : CodeDomSerializer { public DependencyObjectCodeDomSerializer() { } public override object Serialize(IDesignerSerializationManager manager, object obj) { if (manager == null) throw new ArgumentNullException("manager"); if (manager.Context == null) throw new ArgumentException("manager", SR.GetString(SR.Error_MissingContextProperty)); if (obj == null) throw new ArgumentNullException("obj"); DependencyObject dependencyObject = obj as DependencyObject; if (dependencyObject == null) throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(DependencyObject).FullName), "obj"); Activity activity = obj as Activity; if (activity != null) manager.Context.Push(activity); CodeStatementCollection retVal = null; try { if (activity != null) { CodeDomSerializer componentSerializer = manager.GetSerializer(typeof(Component), typeof(CodeDomSerializer)) as CodeDomSerializer; if (componentSerializer == null) throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(CodeDomSerializer).FullName)); retVal = componentSerializer.Serialize(manager, activity) as CodeStatementCollection; } else { retVal = base.Serialize(manager, obj) as CodeStatementCollection; } if (retVal != null) { CodeStatementCollection codeStatements = new CodeStatementCollection(retVal); CodeExpression objectExpression = SerializeToExpression(manager, obj); if (objectExpression != null) { ArrayList propertiesSerialized = new ArrayList(); ListdependencyProperties = new List (dependencyObject.MetaDependencyProperties); foreach (DependencyProperty dp in dependencyObject.DependencyPropertyValues.Keys) { if (dp.IsAttached) { if ((dp.IsEvent && dp.OwnerType.GetField(dp.Name + "Event", BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly) != null) || (!dp.IsEvent && dp.OwnerType.GetField(dp.Name + "Property", BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly) != null)) dependencyProperties.Add(dp); } } foreach (DependencyProperty dependencyProperty in dependencyProperties) { object value = null; if (dependencyObject.IsBindingSet(dependencyProperty)) value = dependencyObject.GetBinding(dependencyProperty); else if(!dependencyProperty.IsEvent) value = dependencyObject.GetValue(dependencyProperty); else value = dependencyObject.GetHandler(dependencyProperty); // Attached property should always be set through SetValue, no matter if it's a meta property or if there is a data context. // Other meta properties will be directly assigned. // Other instance property will go through SetValue if there is a data context or if it's of type Bind. if (value != null && (dependencyProperty.IsAttached || (!dependencyProperty.DefaultMetadata.IsMetaProperty && value is ActivityBind))) { object[] attributes = dependencyProperty.DefaultMetadata.GetAttributes(typeof(DesignerSerializationVisibilityAttribute)); if (attributes.Length > 0) { DesignerSerializationVisibilityAttribute serializationVisibilityAttribute = attributes[0] as DesignerSerializationVisibilityAttribute; if (serializationVisibilityAttribute.Visibility == DesignerSerializationVisibility.Hidden) continue; } // Events of type Bind will go through here. Regular events will go through IEventBindingService. CodeExpression param1 = null; string dependencyPropertyName = dependencyProperty.Name + ((dependencyProperty.IsEvent) ? "Event" : "Property"); FieldInfo fieldInfo = dependencyProperty.OwnerType.GetField(dependencyPropertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); if (fieldInfo != null && !fieldInfo.IsPublic) param1 = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(DependencyProperty)), "FromName", new CodePrimitiveExpression(dependencyProperty.Name), new CodeTypeOfExpression(dependencyProperty.OwnerType)); else param1 = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(dependencyProperty.OwnerType), dependencyPropertyName); CodeExpression param2 = SerializeToExpression(manager, value); //Fields property fails to serialize to expression due to reference not being created, //the actual code for fields are generated in datacontext code generator so we do nothing here if (param1 != null && param2 != null) { CodeMethodInvokeExpression codeMethodInvokeExpr = null; if(value is ActivityBind) codeMethodInvokeExpr = new CodeMethodInvokeExpression(objectExpression, "SetBinding", new CodeExpression[] { param1, new CodeCastExpression(new CodeTypeReference(typeof(ActivityBind)), param2) }); else codeMethodInvokeExpr = new CodeMethodInvokeExpression(objectExpression, (dependencyProperty.IsEvent) ? "AddHandler" : "SetValue", new CodeExpression[] { param1, param2 }); retVal.Add(codeMethodInvokeExpr); // Remove the property set statement for the event which is replaced by the SetValue() expression. foreach (CodeStatement statement in codeStatements) { if (statement is CodeAssignStatement && ((CodeAssignStatement)statement).Left is CodePropertyReferenceExpression) { CodePropertyReferenceExpression prop = ((CodeAssignStatement)statement).Left as CodePropertyReferenceExpression; if (prop.PropertyName == dependencyProperty.Name && prop.TargetObject.Equals(objectExpression)) retVal.Remove(statement); } } } propertiesSerialized.Add(dependencyProperty); } } IEventBindingService eventBindingService = manager.GetService(typeof(IEventBindingService)) as IEventBindingService; if (eventBindingService == null) { // At compile time, we don't have an event binding service. We need to mannually emit the code to add // event handlers. foreach (EventDescriptor eventDesc in TypeDescriptor.GetEvents(dependencyObject)) { string handler = WorkflowMarkupSerializationHelpers.GetEventHandlerName(dependencyObject, eventDesc.Name); if (!string.IsNullOrEmpty(handler)) { CodeEventReferenceExpression eventRef = new CodeEventReferenceExpression(objectExpression, eventDesc.Name); CodeDelegateCreateExpression listener = new CodeDelegateCreateExpression(new CodeTypeReference(eventDesc.EventType), new CodeThisReferenceExpression(), handler); retVal.Add(new CodeAttachEventStatement(eventRef, listener)); } } } // We also need to handle properties of type System.Type. If the value is a design time type, xomlserializer // is not going to be able to deserialize the type. We then store the type name in the user data and // output a "typeof(xxx)" expression using the type name w/o validating the type. if (dependencyObject.UserData.Contains(UserDataKeys.DesignTimeTypeNames)) { Hashtable typeNames = dependencyObject.UserData[UserDataKeys.DesignTimeTypeNames] as Hashtable; foreach (object key in typeNames.Keys) { string propName = null; string ownerTypeName = null; string typeName = typeNames[key] as string; DependencyProperty dependencyProperty = key as DependencyProperty; if (dependencyProperty != null) { if (propertiesSerialized.Contains(dependencyProperty)) continue; object[] attributes = dependencyProperty.DefaultMetadata.GetAttributes(typeof(DesignerSerializationVisibilityAttribute)); if (attributes.Length > 0) { DesignerSerializationVisibilityAttribute serializationVisibilityAttribute = attributes[0] as DesignerSerializationVisibilityAttribute; if (serializationVisibilityAttribute.Visibility == DesignerSerializationVisibility.Hidden) continue; } propName = dependencyProperty.Name; ownerTypeName = dependencyProperty.OwnerType.FullName; } else if (key is string) { int indexOfDot = ((string)key).LastIndexOf('.'); Debug.Assert(indexOfDot != -1, "Wrong property name in DesignTimeTypeNames hashtable."); if (indexOfDot != -1) { ownerTypeName = ((string)key).Substring(0, indexOfDot); propName = ((string)key).Substring(indexOfDot + 1); } } if (!string.IsNullOrEmpty(typeName) && !string.IsNullOrEmpty(propName) && !string.IsNullOrEmpty(ownerTypeName)) { if (ownerTypeName == obj.GetType().FullName) { // Property is not an attached property. Serialize using regular property set expression. CodePropertyReferenceExpression propertyRef = new CodePropertyReferenceExpression(objectExpression, propName); retVal.Add(new CodeAssignStatement(propertyRef, new CodeTypeOfExpression(typeName))); } else { // This is an attached property. Serialize using SetValue() expression. CodeExpression param1 = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(ownerTypeName), propName + "Property"); CodeExpression param2 = new CodeTypeOfExpression(typeName); retVal.Add(new CodeMethodInvokeExpression(objectExpression, "SetValue", new CodeExpression[] { param1, param2 })); } } } } } } } finally { if (activity != null) { object pushedActivity = manager.Context.Pop(); System.Diagnostics.Debug.Assert(pushedActivity == activity); } } return retVal; } } #endregion } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SqlInternalConnectionSmi.cs
- ProgressBarHighlightConverter.cs
- basecomparevalidator.cs
- IList.cs
- UserThread.cs
- DeclarativeExpressionConditionDeclaration.cs
- TemplatedWizardStep.cs
- base64Transforms.cs
- OracleBoolean.cs
- ScopedKnownTypes.cs
- MonitoringDescriptionAttribute.cs
- ProcessInfo.cs
- ExtensionQuery.cs
- WinEventQueueItem.cs
- ArcSegment.cs
- XmlDocumentSurrogate.cs
- BoolExpression.cs
- TogglePattern.cs
- SqlServer2KCompatibilityCheck.cs
- MethodBuilderInstantiation.cs
- HttpModuleAction.cs
- HttpCachePolicy.cs
- RewritingValidator.cs
- WsatTransactionHeader.cs
- BooleanFunctions.cs
- FontInfo.cs
- CodeTypeMemberCollection.cs
- TextParaClient.cs
- DockPattern.cs
- ACE.cs
- Underline.cs
- PrintPreviewGraphics.cs
- BamlStream.cs
- DbMetaDataCollectionNames.cs
- ExtensionMethods.cs
- BasicBrowserDialog.designer.cs
- ApplicationDirectoryMembershipCondition.cs
- EventWaitHandle.cs
- contentDescriptor.cs
- VariableReference.cs
- ShadowGlyph.cs
- Crypto.cs
- SqlLiftIndependentRowExpressions.cs
- ObjectTag.cs
- RoleManagerSection.cs
- NamespaceTable.cs
- SQlBooleanStorage.cs
- UmAlQuraCalendar.cs
- DashStyle.cs
- SqlDependency.cs
- FormViewUpdateEventArgs.cs
- DropShadowBitmapEffect.cs
- TextPenaltyModule.cs
- PageEventArgs.cs
- StopStoryboard.cs
- EventData.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- DataGridViewCellFormattingEventArgs.cs
- XmlAtomErrorReader.cs
- VisualProxy.cs
- ExpressionWriter.cs
- CharEnumerator.cs
- GlobalizationSection.cs
- _TimerThread.cs
- StaticFileHandler.cs
- OdbcStatementHandle.cs
- EventProviderClassic.cs
- CodeParameterDeclarationExpression.cs
- ContravarianceAdapter.cs
- EventSetter.cs
- _Rfc2616CacheValidators.cs
- KeyGestureValueSerializer.cs
- MergablePropertyAttribute.cs
- FontSource.cs
- SafeProcessHandle.cs
- DataGridViewTextBoxEditingControl.cs
- serverconfig.cs
- Directory.cs
- CommandManager.cs
- ObjectStateManagerMetadata.cs
- ErrorFormatterPage.cs
- ListViewUpdateEventArgs.cs
- NodeCounter.cs
- WindowsRichEdit.cs
- SqlBulkCopyColumnMapping.cs
- WebOperationContext.cs
- QuaternionAnimationBase.cs
- HttpListenerRequest.cs
- CallbackException.cs
- HierarchicalDataSourceConverter.cs
- ImageSourceConverter.cs
- SqlVersion.cs
- Debug.cs
- DataAccessException.cs
- CommandCollectionEditor.cs
- MenuAutomationPeer.cs
- ExpandCollapsePattern.cs
- PermissionSetTriple.cs
- PopupRootAutomationPeer.cs
- ButtonStandardAdapter.cs