Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / Services / Monitoring / system / Diagnosticts / Design / StringDictionaryCodeDomSerializer.cs / 1 / StringDictionaryCodeDomSerializer.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
*/
namespace System.Diagnostics.Design {
using System;
using System.Design;
using System.CodeDom;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Reflection;
using System.ComponentModel.Design.Serialization;
using System.Collections.Specialized;
///
///
/// This serializer serializes string dictionaries.
///
internal class StringDictionaryCodeDomSerializer : CodeDomSerializer {
///
///
/// This method takes a CodeDomObject and deserializes into a real object.
/// We don't do anything here.
///
public override object Deserialize(IDesignerSerializationManager manager, object codeObject) {
Debug.Fail("Don't expect this to be called.");
return null;
}
///
///
/// Serializes the given object into a CodeDom object.
///
public override object Serialize(IDesignerSerializationManager manager, object value) {
object result = null;
StringDictionary dictionary = value as StringDictionary;
if (dictionary != null) {
object context = manager.Context.Current;
ExpressionContext exp = context as ExpressionContext;
if (exp != null) {
if (exp.Owner == value) {
context = exp.Expression;
}
}
// we can only serialize if we have a CodePropertyReferenceExpression
CodePropertyReferenceExpression propRef = context as CodePropertyReferenceExpression;
if (propRef != null) {
// get the object with the property we're setting
object targetObject = DeserializeExpression(manager, null, propRef.TargetObject);
if (targetObject != null) {
// get the PropertyDescriptor of the property we're setting
PropertyDescriptor prop = TypeDescriptor.GetProperties(targetObject)[propRef.PropertyName];
if (prop != null) {
// okay, we have the property and we have the StringDictionary, now we generate
// a line like this (c# example):
// myObject.strDictProp["key"] = "value";
// for each key/value pair in the StringDictionary
CodeStatementCollection statements = new CodeStatementCollection();
CodeMethodReferenceExpression methodRef = new CodeMethodReferenceExpression(propRef, "Add");
foreach (DictionaryEntry entry in dictionary) {
// serialize the key (in most languages this will look like "key")
CodeExpression serializedKey = SerializeToExpression(manager, entry.Key);
// serialize the value (in most languages this will look like "value")
CodeExpression serializedValue = SerializeToExpression(manager, entry.Value);
// serialize the method call (prop.Add("key", "value"))
if (serializedKey != null && serializedValue != null) {
CodeMethodInvokeExpression statement = new CodeMethodInvokeExpression();
statement.Method = methodRef;
statement.Parameters.Add(serializedKey);
statement.Parameters.Add(serializedValue);
statements.Add(statement);
}
}
result = statements;
}
}
}
}
else {
// 'value' is not a StringDictionary. What should we do?
}
return result;
}
}
}
// 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
- InternalRelationshipCollection.cs
- NullableBoolConverter.cs
- AutomationIdentifierGuids.cs
- RequestCachingSection.cs
- HttpRequest.cs
- SecurityElement.cs
- Underline.cs
- StickyNote.cs
- ServiceControllerDesigner.cs
- TextParentUndoUnit.cs
- KeySpline.cs
- CompositeDataBoundControl.cs
- RuntimeArgument.cs
- BaseProcessor.cs
- NoPersistProperty.cs
- SharedRuntimeState.cs
- RecognizedWordUnit.cs
- SecureUICommand.cs
- WorkflowMessageEventArgs.cs
- OperationResponse.cs
- AssemblyContextControlItem.cs
- TypeBuilder.cs
- JavaScriptSerializer.cs
- CompositeFontParser.cs
- ObjectPropertyMapping.cs
- GraphicsContainer.cs
- _SafeNetHandles.cs
- WS2007HttpBindingElement.cs
- CacheAxisQuery.cs
- SrgsItemList.cs
- Token.cs
- TransformPattern.cs
- SymDocumentType.cs
- CollaborationHelperFunctions.cs
- SQLMembershipProvider.cs
- SymbolMethod.cs
- Comparer.cs
- CompleteWizardStep.cs
- TextProviderWrapper.cs
- SqlDataSourceFilteringEventArgs.cs
- Rect3DValueSerializer.cs
- OuterGlowBitmapEffect.cs
- TypedElement.cs
- PolyLineSegment.cs
- CancelEventArgs.cs
- ReferencedAssembly.cs
- SafeEventLogWriteHandle.cs
- DataGridViewColumnDividerDoubleClickEventArgs.cs
- DataServiceExpressionVisitor.cs
- TemplatePropertyEntry.cs
- CollectionViewGroupRoot.cs
- IUnknownConstantAttribute.cs
- RadioButton.cs
- WebConfigurationHost.cs
- Base64Decoder.cs
- StorageRoot.cs
- FormsAuthenticationConfiguration.cs
- WebPartAddingEventArgs.cs
- BaseDataList.cs
- ResourceDisplayNameAttribute.cs
- DoWorkEventArgs.cs
- SingleAnimationUsingKeyFrames.cs
- TimelineCollection.cs
- TemplateInstanceAttribute.cs
- LastQueryOperator.cs
- BypassElementCollection.cs
- NameSpaceExtractor.cs
- __TransparentProxy.cs
- SystemIcmpV4Statistics.cs
- ScriptMethodAttribute.cs
- SqlProviderServices.cs
- SoapAttributeOverrides.cs
- AliasedSlot.cs
- ParamArrayAttribute.cs
- HtmlTableCell.cs
- Internal.cs
- Scene3D.cs
- WebPartConnection.cs
- DataGridRelationshipRow.cs
- AutoGeneratedField.cs
- MonitoringDescriptionAttribute.cs
- DoubleAnimationBase.cs
- Input.cs
- ContentFilePart.cs
- ClientTarget.cs
- ScrollItemPattern.cs
- HierarchicalDataBoundControl.cs
- SortedSetDebugView.cs
- LinqDataSourceStatusEventArgs.cs
- MetadataItemSerializer.cs
- ProtocolsConfigurationHandler.cs
- ConditionChanges.cs
- ConfigurationValue.cs
- CommentEmitter.cs
- ContactManager.cs
- TypeAccessException.cs
- PeerEndPoint.cs
- _NegotiateClient.cs
- RelationshipNavigation.cs
- Int16.cs