Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WF / Common / AuthoringOM / Serializer / ListSurrogate.cs / 1305376 / ListSurrogate.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 ListSurrogate internal sealed class ListSurrogate : ISerializationSurrogate { internal ListSurrogate() { } void ISerializationSurrogate.GetObjectData(object obj, SerializationInfo info, StreamingContext context) { if (!obj.GetType().IsGenericType || obj.GetType().GetGenericTypeDefinition() != typeof(List<>)) throw new ArgumentException(SR.GetString(SR.Error_InvalidArgumentValue), "obj"); Type[] args = obj.GetType().GetGenericArguments(); if (args.Length != 1) throw new ArgumentException(SR.GetString(SR.Error_InvalidArgumentValue), "obj"); ArrayList items = new ArrayList(obj as IList); if (items.Count == 1) info.AddValue("item", items[0]); else info.AddValue("items", items.ToArray()); info.AddValue("itemType", args[0]); info.SetType(typeof(ListRef)); } object ISerializationSurrogate.SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) { return null; } #region ListRef [Serializable] private sealed class ListRef : IObjectReference, IDeserializationCallback { [OptionalField] private IList items = null; [OptionalField] private object item = null; private Type itemType = null; [NonSerialized] private object list = null; Object IObjectReference.GetRealObject(StreamingContext context) { if (this.list == null) { Type listType = typeof(List).GetGenericTypeDefinition().MakeGenericType(itemType); this.list = listType.GetConstructor(Type.EmptyTypes).Invoke(null); } return this.list; } void IDeserializationCallback.OnDeserialization(Object sender) { if (this.list != null) { MethodInfo addMethod = this.list.GetType().GetMethod("Add"); if (addMethod == null) throw new NullReferenceException("addMethod"); if (this.items != null) { for (int n = 0; n < items.Count; n++) addMethod.Invoke(this.list, new object[] { this.items[n] }); } else { addMethod.Invoke(this.list, new object[] { this.item }); } this.list = 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 ListSurrogate internal sealed class ListSurrogate : ISerializationSurrogate { internal ListSurrogate() { } void ISerializationSurrogate.GetObjectData(object obj, SerializationInfo info, StreamingContext context) { if (!obj.GetType().IsGenericType || obj.GetType().GetGenericTypeDefinition() != typeof(List<>)) throw new ArgumentException(SR.GetString(SR.Error_InvalidArgumentValue), "obj"); Type[] args = obj.GetType().GetGenericArguments(); if (args.Length != 1) throw new ArgumentException(SR.GetString(SR.Error_InvalidArgumentValue), "obj"); ArrayList items = new ArrayList(obj as IList); if (items.Count == 1) info.AddValue("item", items[0]); else info.AddValue("items", items.ToArray()); info.AddValue("itemType", args[0]); info.SetType(typeof(ListRef)); } object ISerializationSurrogate.SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) { return null; } #region ListRef [Serializable] private sealed class ListRef : IObjectReference, IDeserializationCallback { [OptionalField] private IList items = null; [OptionalField] private object item = null; private Type itemType = null; [NonSerialized] private object list = null; Object IObjectReference.GetRealObject(StreamingContext context) { if (this.list == null) { Type listType = typeof(List ).GetGenericTypeDefinition().MakeGenericType(itemType); this.list = listType.GetConstructor(Type.EmptyTypes).Invoke(null); } return this.list; } void IDeserializationCallback.OnDeserialization(Object sender) { if (this.list != null) { MethodInfo addMethod = this.list.GetType().GetMethod("Add"); if (addMethod == null) throw new NullReferenceException("addMethod"); if (this.items != null) { for (int n = 0; n < items.Count; n++) addMethod.Invoke(this.list, new object[] { this.items[n] }); } else { addMethod.Invoke(this.list, new object[] { this.item }); } this.list = 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
- WizardForm.cs
- TogglePattern.cs
- CodeDirectoryCompiler.cs
- TemplateBamlTreeBuilder.cs
- LinkClickEvent.cs
- ColumnResizeAdorner.cs
- AutomationPatternInfo.cs
- CallSiteBinder.cs
- CompilationPass2TaskInternal.cs
- DockPattern.cs
- ResourceReferenceExpressionConverter.cs
- TreeNodeStyle.cs
- SafeMarshalContext.cs
- ObjectResult.cs
- NavigateEvent.cs
- DelegateBodyWriter.cs
- DPCustomTypeDescriptor.cs
- SpellerError.cs
- XmlMemberMapping.cs
- BufferedGraphicsContext.cs
- GenerateTemporaryTargetAssembly.cs
- BookmarkEventArgs.cs
- NameHandler.cs
- Soap12ServerProtocol.cs
- JsonDeserializer.cs
- MarkupCompiler.cs
- OuterGlowBitmapEffect.cs
- ComPlusServiceLoader.cs
- Vector3DCollection.cs
- XDeferredAxisSource.cs
- Image.cs
- SettingsContext.cs
- PageParserFilter.cs
- XmlArrayAttribute.cs
- SourceInterpreter.cs
- Pkcs7Signer.cs
- FieldDescriptor.cs
- _ServiceNameStore.cs
- PackWebResponse.cs
- ZipIOExtraField.cs
- TransformPatternIdentifiers.cs
- IERequestCache.cs
- TreeIterators.cs
- WebPartCancelEventArgs.cs
- CompositeFontFamily.cs
- ApplicationId.cs
- DiagnosticTrace.cs
- ApplicationInfo.cs
- WhitespaceRuleReader.cs
- NumberFunctions.cs
- GlobalProxySelection.cs
- FilterEventArgs.cs
- ZipIOExtraFieldZip64Element.cs
- CroppedBitmap.cs
- ListView.cs
- DynamicDataManager.cs
- WsatAdminException.cs
- MappingModelBuildProvider.cs
- SqlMethodTransformer.cs
- GridViewRow.cs
- SrgsDocumentParser.cs
- TreeNodeSelectionProcessor.cs
- ResXFileRef.cs
- ToolboxControl.cs
- CellTreeNodeVisitors.cs
- OracleParameterBinding.cs
- ImpersonationContext.cs
- HtmlInputReset.cs
- WindowsFormsSynchronizationContext.cs
- DynamicRendererThreadManager.cs
- SignatureGenerator.cs
- DataSourceControl.cs
- DbConnectionPoolOptions.cs
- SafeRightsManagementQueryHandle.cs
- ItemsControlAutomationPeer.cs
- MissingFieldException.cs
- LinqDataSourceSelectEventArgs.cs
- StatusCommandUI.cs
- DataSourceProvider.cs
- XMLSchema.cs
- GridEntry.cs
- ByteStack.cs
- OleDbMetaDataFactory.cs
- FileReservationCollection.cs
- RuntimeWrappedException.cs
- VisualTreeUtils.cs
- BuildProvidersCompiler.cs
- SizeKeyFrameCollection.cs
- FixedHyperLink.cs
- TraceHwndHost.cs
- LineUtil.cs
- DataGridViewRowConverter.cs
- Globals.cs
- NotSupportedException.cs
- GlobalizationSection.cs
- _AuthenticationState.cs
- TraceLog.cs
- SystemBrushes.cs
- MatrixAnimationUsingKeyFrames.cs
- ClientSponsor.cs