Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / CompMod / System / ComponentModel / Design / Serialization / EnumCodeDomSerializer.cs / 1 / EnumCodeDomSerializer.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /* */ namespace System.ComponentModel.Design.Serialization { using System; using System.CodeDom; using System.Collections; using System.ComponentModel; using System.ComponentModel.Design; using System.Diagnostics; using System.Reflection; ////// /// Code model serializer for enum types. /// internal class EnumCodeDomSerializer : CodeDomSerializer { private static EnumCodeDomSerializer defaultSerializer; ////// /// Retrieves a default static instance of this serializer. /// internal new static EnumCodeDomSerializer Default { get { if (defaultSerializer == null) { defaultSerializer = new EnumCodeDomSerializer(); } return defaultSerializer; } } ////// /// Serializes the given object into a CodeDom object. /// public override object Serialize(IDesignerSerializationManager manager, object value) { CodeExpression expression = null; using (TraceScope("EnumCodeDomSerializer::Serialize")) { Trace("Type: {0}", (value == null ? "(null)" : value.GetType().Name)); if (value is Enum) { bool needCast = false; Enum[] values; TypeConverter converter = TypeDescriptor.GetConverter(value); if (converter != null && converter.CanConvertTo(typeof(Enum[]))) { values = (Enum[])converter.ConvertTo(value, typeof(Enum[])); needCast = (values.Length > 1); } else { values = new Enum[] {(Enum)value}; needCast = true; } // EnumConverter (and anything that is overridden to support enums) // should be providing us a conversion to Enum[] for flag styles. // If it doesn't, we will emit a warning and just do a cast from the // enum value. CodeTypeReferenceExpression enumType = new CodeTypeReferenceExpression(value.GetType()); // If names is of length 1, then this is a simple field reference. Otherwise, // it is an or-d combination of expressions. // TraceIf(values.Length == 1, "Single field entity."); TraceIf(values.Length > 1, "Multi field entity."); // We now need to serialize the enum terms as fields. We new up an EnumConverter to do // that. We cannot use the type's own converter since it might have a different string // representation for its values. Hardcoding is okay in this case, since all we want is // the enum's field name. Simply doing ToString() will not give us any validation. TypeConverter enumConverter = new EnumConverter(value.GetType()); foreach (Enum term in values) { string termString = (enumConverter != null) ? enumConverter.ConvertToString(term) : null; CodeExpression newExpression = !String.IsNullOrEmpty(termString) ? new CodeFieldReferenceExpression(enumType, termString) : null; if (newExpression != null) { if (expression == null) { expression = newExpression; } else { expression = new CodeBinaryOperatorExpression(expression, CodeBinaryOperatorType.BitwiseOr, newExpression); } } } // If we had to combine multiple names, wrap the result in an appropriate cast. // if (expression != null && needCast) { expression = new CodeCastExpression(value.GetType(), expression); } } else { Debug.Fail("Enum serializer called for non-enum object."); TraceError("Enum serializer called for non-enum object {0}", (value == null ? "(null)" : value.GetType().Name)); } } return expression; } } } // 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
- StreamInfo.cs
- ButtonBase.cs
- SmtpSpecifiedPickupDirectoryElement.cs
- TextServicesDisplayAttributePropertyRanges.cs
- XhtmlBasicPhoneCallAdapter.cs
- ReflectionTypeLoadException.cs
- NamedPipeTransportSecurityElement.cs
- PipelineModuleStepContainer.cs
- AnimatedTypeHelpers.cs
- DbConnectionPoolGroup.cs
- DrawingAttributesDefaultValueFactory.cs
- DesignerLabelAdapter.cs
- AtlasWeb.Designer.cs
- RoleServiceManager.cs
- shaperfactoryquerycacheentry.cs
- FormsAuthentication.cs
- CornerRadius.cs
- _NTAuthentication.cs
- SizeAnimationClockResource.cs
- ContentTextAutomationPeer.cs
- KeyPullup.cs
- NameValueConfigurationCollection.cs
- PrintPreviewDialog.cs
- XmlSchemaImport.cs
- KeyedHashAlgorithm.cs
- InvokeMemberBinder.cs
- SharedConnectionListener.cs
- CryptoConfig.cs
- LayoutInformation.cs
- basemetadatamappingvisitor.cs
- DisplayMemberTemplateSelector.cs
- Debug.cs
- JpegBitmapDecoder.cs
- SqlNode.cs
- HostingPreferredMapPath.cs
- _NetRes.cs
- PriorityQueue.cs
- MarkupCompilePass1.cs
- DefaultAssemblyResolver.cs
- WindowInteropHelper.cs
- StreamGeometry.cs
- RectAnimationUsingKeyFrames.cs
- CrossContextChannel.cs
- NumericUpDownAcceleration.cs
- x509utils.cs
- XmlSchemaGroup.cs
- BuilderInfo.cs
- TextMarkerSource.cs
- PolicyException.cs
- MembershipUser.cs
- StagingAreaInputItem.cs
- PenThreadPool.cs
- ManagementObjectCollection.cs
- DetailsViewDesigner.cs
- RegexEditorDialog.cs
- ServiceOperationViewControl.cs
- Label.cs
- CapacityStreamGeometryContext.cs
- FrameworkContentElement.cs
- WSSecureConversationFeb2005.cs
- ProcessInfo.cs
- ManipulationDelta.cs
- Pkcs7Recipient.cs
- ComponentChangingEvent.cs
- SettingsProviderCollection.cs
- DesignerDataSchemaClass.cs
- TextDecorationLocationValidation.cs
- AppDomainGrammarProxy.cs
- ComponentCache.cs
- NativeObjectSecurity.cs
- PanelStyle.cs
- DBProviderConfigurationHandler.cs
- CollectionEditorDialog.cs
- GlobalizationSection.cs
- Typography.cs
- SqlRemoveConstantOrderBy.cs
- TreeChangeInfo.cs
- TextEditorMouse.cs
- XmlSchemaDocumentation.cs
- FtpWebResponse.cs
- ValueSerializer.cs
- TreeNodeSelectionProcessor.cs
- HitTestFilterBehavior.cs
- PixelFormatConverter.cs
- Globals.cs
- DebugViewWriter.cs
- PropertyManager.cs
- DataObject.cs
- GenericParameterDataContract.cs
- GuidelineSet.cs
- SoapHeader.cs
- CompiledQueryCacheEntry.cs
- UIPropertyMetadata.cs
- PublisherIdentityPermission.cs
- TiffBitmapEncoder.cs
- CollectionDataContractAttribute.cs
- DeviceContext.cs
- LabelDesigner.cs
- GetCryptoTransformRequest.cs
- EpmAttributeNameBuilder.cs