Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WF / Common / AuthoringOM / Serializer / DictionarySurrogate.cs / 1305376 / DictionarySurrogate.cs
namespace System.Workflow.ComponentModel.Serialization { using System; using System.Xml; using System.Runtime.Serialization; using System.Reflection; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Collections; using System.Collections.Generic; #region DictionarySurrogate internal sealed class DictionarySurrogate : ISerializationSurrogate { void ISerializationSurrogate.GetObjectData(object obj, SerializationInfo info, StreamingContext context) { if (!obj.GetType().IsGenericType || obj.GetType().GetGenericTypeDefinition() != typeof(Dictionary<,>)) throw new ArgumentException(SR.GetString(SR.Error_InvalidArgumentValue), "obj"); Type[] args = obj.GetType().GetGenericArguments(); if (args.Length != 2) throw new ArgumentException(SR.GetString(SR.Error_InvalidArgumentValue), "obj"); PropertyInfo keysProperty = obj.GetType().GetProperty("Keys"); if (keysProperty == null) throw new NullReferenceException("keysProperty"); ArrayList keys = new ArrayList(keysProperty.GetValue(obj, null) as ICollection); PropertyInfo valuesProperty = obj.GetType().GetProperty("Values"); if (valuesProperty == null) throw new NullReferenceException("valuesProperty"); ArrayList values = new ArrayList(valuesProperty.GetValue(obj, null) as ICollection); if (keys.Count == 1) { info.AddValue("key", keys[0]); info.AddValue("value", values[0]); } else if (keys.Count > 1) { info.AddValue("keys", keys.ToArray()); info.AddValue("values", values.ToArray()); } info.AddValue("keyType", args[0]); info.AddValue("valueType", args[1]); info.SetType(typeof(DictionaryRef)); } object ISerializationSurrogate.SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) { return null; } #region DictionaryRef [Serializable] private sealed class DictionaryRef : IObjectReference, IDeserializationCallback { [OptionalField] private IList keys = null; [OptionalField] private IList values = null; [OptionalField] private object key = null; [OptionalField] private object value = null; private Type keyType = null; private Type valueType = null; [NonSerialized] private object dictionary = null; Object IObjectReference.GetRealObject(StreamingContext context) { if (this.dictionary == null) { Type dictionaryType = typeof(Dictionary).GetGenericTypeDefinition().MakeGenericType(keyType, valueType); this.dictionary = dictionaryType.GetConstructor(Type.EmptyTypes).Invoke(null); } return this.dictionary; } void IDeserializationCallback.OnDeserialization(Object sender) { if (this.dictionary != null) { MethodInfo addMethod = this.dictionary.GetType().GetMethod("Add"); if (addMethod == null) throw new NullReferenceException("addMethod"); object[] kvp = new object[2]; if (this.keys != null) { for (int index = 0; index < this.keys.Count; index++) { kvp[0] = this.keys[index]; kvp[1] = this.values[index]; addMethod.Invoke(this.dictionary, kvp); } } else if (this.key != null) { kvp[0] = this.key; kvp[1] = this.value; addMethod.Invoke(this.dictionary, kvp); } } this.dictionary = null; } } #endregion } #endregion } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. namespace System.Workflow.ComponentModel.Serialization { using System; using System.Xml; using System.Runtime.Serialization; using System.Reflection; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Collections; using System.Collections.Generic; #region DictionarySurrogate internal sealed class DictionarySurrogate : ISerializationSurrogate { void ISerializationSurrogate.GetObjectData(object obj, SerializationInfo info, StreamingContext context) { if (!obj.GetType().IsGenericType || obj.GetType().GetGenericTypeDefinition() != typeof(Dictionary<,>)) throw new ArgumentException(SR.GetString(SR.Error_InvalidArgumentValue), "obj"); Type[] args = obj.GetType().GetGenericArguments(); if (args.Length != 2) throw new ArgumentException(SR.GetString(SR.Error_InvalidArgumentValue), "obj"); PropertyInfo keysProperty = obj.GetType().GetProperty("Keys"); if (keysProperty == null) throw new NullReferenceException("keysProperty"); ArrayList keys = new ArrayList(keysProperty.GetValue(obj, null) as ICollection); PropertyInfo valuesProperty = obj.GetType().GetProperty("Values"); if (valuesProperty == null) throw new NullReferenceException("valuesProperty"); ArrayList values = new ArrayList(valuesProperty.GetValue(obj, null) as ICollection); if (keys.Count == 1) { info.AddValue("key", keys[0]); info.AddValue("value", values[0]); } else if (keys.Count > 1) { info.AddValue("keys", keys.ToArray()); info.AddValue("values", values.ToArray()); } info.AddValue("keyType", args[0]); info.AddValue("valueType", args[1]); info.SetType(typeof(DictionaryRef)); } object ISerializationSurrogate.SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) { return null; } #region DictionaryRef [Serializable] private sealed class DictionaryRef : IObjectReference, IDeserializationCallback { [OptionalField] private IList keys = null; [OptionalField] private IList values = null; [OptionalField] private object key = null; [OptionalField] private object value = null; private Type keyType = null; private Type valueType = null; [NonSerialized] private object dictionary = null; Object IObjectReference.GetRealObject(StreamingContext context) { if (this.dictionary == null) { Type dictionaryType = typeof(Dictionary ).GetGenericTypeDefinition().MakeGenericType(keyType, valueType); this.dictionary = dictionaryType.GetConstructor(Type.EmptyTypes).Invoke(null); } return this.dictionary; } void IDeserializationCallback.OnDeserialization(Object sender) { if (this.dictionary != null) { MethodInfo addMethod = this.dictionary.GetType().GetMethod("Add"); if (addMethod == null) throw new NullReferenceException("addMethod"); object[] kvp = new object[2]; if (this.keys != null) { for (int index = 0; index < this.keys.Count; index++) { kvp[0] = this.keys[index]; kvp[1] = this.values[index]; addMethod.Invoke(this.dictionary, kvp); } } else if (this.key != null) { kvp[0] = this.key; kvp[1] = this.value; addMethod.Invoke(this.dictionary, kvp); } } this.dictionary = null; } } #endregion } #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
- TextTreeNode.cs
- StickyNoteHelper.cs
- GenericAuthenticationEventArgs.cs
- Matrix3D.cs
- FileIOPermission.cs
- FixedSOMFixedBlock.cs
- AmbientLight.cs
- RankException.cs
- InputEventArgs.cs
- TemplateParser.cs
- MatrixKeyFrameCollection.cs
- LineServicesCallbacks.cs
- Completion.cs
- StatusBarPanelClickEvent.cs
- DataGridBoolColumn.cs
- ExclusiveTcpListener.cs
- ServiceReference.cs
- TdsRecordBufferSetter.cs
- ConditionCollection.cs
- FieldDescriptor.cs
- TrackBarDesigner.cs
- activationcontext.cs
- BitStream.cs
- Parallel.cs
- WizardSideBarListControlItemEventArgs.cs
- CompilationRelaxations.cs
- _NativeSSPI.cs
- NewExpression.cs
- DoubleCollection.cs
- SourceFileInfo.cs
- BroadcastEventHelper.cs
- EndpointDispatcher.cs
- Transform3D.cs
- DeferredElementTreeState.cs
- SimpleRecyclingCache.cs
- EntityCommandCompilationException.cs
- SqlXml.cs
- TextDecorationCollection.cs
- CheckBoxStandardAdapter.cs
- Perspective.cs
- _Events.cs
- _ProxyRegBlob.cs
- DateTimeFormat.cs
- DefinitionProperties.cs
- SessionStateContainer.cs
- RequestResizeEvent.cs
- OverrideMode.cs
- MSAANativeProvider.cs
- SoapException.cs
- COM2ComponentEditor.cs
- FlowPosition.cs
- StrongNameUtility.cs
- WindowsStatusBar.cs
- SoapCodeExporter.cs
- SecurityIdentifierConverter.cs
- Module.cs
- graph.cs
- MinimizableAttributeTypeConverter.cs
- NativeMethodsCLR.cs
- SamlSerializer.cs
- ResourceIDHelper.cs
- RunWorkerCompletedEventArgs.cs
- ContainerFilterService.cs
- UxThemeWrapper.cs
- SafeArrayTypeMismatchException.cs
- MetadataItem_Static.cs
- ProcessModelSection.cs
- EncodingTable.cs
- WhiteSpaceTrimStringConverter.cs
- UnhandledExceptionEventArgs.cs
- Visual3D.cs
- InputProcessorProfilesLoader.cs
- CollaborationHelperFunctions.cs
- VisualTarget.cs
- HttpApplicationStateWrapper.cs
- assertwrapper.cs
- PeekCompletedEventArgs.cs
- SHA512.cs
- DesignTimeData.cs
- SerializationFieldInfo.cs
- AssemblyCollection.cs
- RoutedEventArgs.cs
- System.Data.OracleClient_BID.cs
- TrackingWorkflowEventArgs.cs
- TreeViewItem.cs
- Popup.cs
- SiteMapDataSourceView.cs
- InputMethodStateTypeInfo.cs
- FormViewDeleteEventArgs.cs
- GeneralTransform3DTo2DTo3D.cs
- FreezableDefaultValueFactory.cs
- COM2ColorConverter.cs
- XmlILConstructAnalyzer.cs
- _FtpControlStream.cs
- MultipleViewProviderWrapper.cs
- XmlSchemaAttributeGroupRef.cs
- XmlCustomFormatter.cs
- HelpOperationInvoker.cs
- CompoundFileReference.cs
- wgx_exports.cs