'宣言 Public Overloads Shared Function DeserializeObjectProperty( _ ByVal info As SerializationInfo, _ ByVal name As String _ ) As Object
public static object DeserializeObjectProperty( SerializationInfo info, string name )
Imports System.Runtime.Serialization Imports System.Drawing Imports Infragistics.Shared Imports Infragistics.Shared.Serialization Imports Infragistics.Win <Serializable()> Public Class MySerializableClass Inherits KeyedSubObjectBase Implements ISerializable Private enabled As Boolean = True Private height As Integer = 0 Private location As Point Private borderStyle As UIElementBorderStyle = UIElementBorderStyle.Solid Public Sub New(ByVal keyValue As String) MyBase.New(keyValue) End Sub ' Special de-serialization constructor. Private Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) ' Iterate over the entries. This is considerably faster ' than calling the various 'Get...' methods exposed by ' the SerializationInfo object for the following reasons: ' 1. Usually not every property is persisted. For example, ' an object might have 50 properties but only 5 that had ' non-default values when it was serialized out. ' 2. When you call a 'Get...' method, if the value was ' not persisted an exception is thrown which is ' relatively expensive. ' 3. Switch statements with strings are very efficient ' because they take advantage of string internment ' and are doing object reference comparisons instead ' of string comparisons. Dim entry As SerializationEntry For Each entry In info ' Use the DeserializeProperty static method to de-serialize ' properties. ' This works for both binary and SOAP formatters. ' Note: the last paramter into this method takes the ' defualt value that will be returned if there is a ' problem converting the value to the requested type. Select Case entry.Name Case "BorderStyle" Me.borderStyle = Utils.DeserializeProperty(entry, GetType(UIElementBorderStyle), Me.borderStyle) Case "Enabled" Me.enabled = Utils.DeserializeProperty(entry, GetType(Boolean), Me.enabled) Case "Height" Me.height = Utils.DeserializeProperty(entry, GetType(Integer), Me.height) Case "Key" Me.Key = Utils.DeserializeProperty(entry, GetType(String), Me.Key) Case "Location" Me.location = Utils.DeserializeProperty(entry, GetType(Point), Me.location) ' Use the protected const (defined in SubObjectBase) ' 'TagSerializationName' which was used by the ' 'SerializeTag' method. It has a value of "Tag". Case MySerializableClass.TagSerializationName ' Use SubObjectBase's DeserializeTag method to de-serialize the tag property. MyBase.DeserializeTag(entry) Case Else Debug.Assert(False, "Invalid entry in MySerializableClass de-serialization ctor") End Select Next End Sub ' Called to serialize the object Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData ' Use the SerializeProperty static method to serialize properties. ' This works for both binary and SOAP formatters If Not Me.borderStyle = UIElementBorderStyle.Solid Then Utils.SerializeProperty(info, "BorderStyle", Me.borderStyle) End If If Not Me.enabled Then Utils.SerializeProperty(info, "Enabled", Me.enabled) End If If Me.height <> 0 Then Utils.SerializeProperty(info, "Height", Me.height) End If If Not Me.location.IsEmpty Then Utils.SerializeProperty(info, "Location", Me.location) End If If Me.Key.Length > 0 Then Utils.SerializeProperty(info, "Key", Me.Key) End If ' Call the SubObjectBase's SerializeTag method which will only ' serialize the tag if it makes sense. MyBase.SerializeTag(info) End Sub End Class
using System; using System.Drawing; using System.Diagnostics; using System.Runtime.Serialization; using Infragistics.Shared; using Infragistics.Shared.Serialization; using Infragistics.Win; namespace SharedSnippets { [Serializable()] public class MySerializableClass : KeyedSubObjectBase, ISerializable { private UIElementBorderStyle borderStyle = UIElementBorderStyle.Solid; private bool enabled = true; private int height = 0; private Point location; public MySerializableClass( string key ) : base( key ) { } // Special de-serialization constructor. private MySerializableClass(SerializationInfo info, StreamingContext context) { // Iterate over the entries. This is considerably faster // than calling the various 'Get...' methods exposed by // the SerializationInfo object for the following reasons: // 1. Usually not every property is persisted. For example, // an object might have 50 properties but only 5 that had // non-default values when it was serialized out. // 2. When you call a 'Get...' method, if the value was // not persisted an exception is thrown which is // relatively expensive. // 3. Switch statements with strings are very efficient // because they take advantage of string internment // and are doing object reference comparisons instead // of string comparisons. foreach( SerializationEntry entry in info ) { switch (entry.Name) { // Use the DeserializeProperty static method to de-serialize // properties. // This works for both binary and SOAP formatters. // Note: the last paramter into this method takes the // defualt value that will be returned if there is a // problem converting the value to the requested type. case "BorderStyle": this.borderStyle = (UIElementBorderStyle)Utils.DeserializeProperty( entry, typeof(UIElementBorderStyle), this.borderStyle ); break; case "Enabled": this.enabled = (bool)Utils.DeserializeProperty( entry, typeof(bool), this.enabled ); break; case "Height": this.height = (int)Utils.DeserializeProperty( entry, typeof(int), this.height ); break; case "Key": this.Key = (string)Utils.DeserializeProperty( entry, typeof(string), this.Key ); break; case "Location": this.location = (Point)Utils.DeserializeProperty( entry, typeof(Point), this.location ); break; // Use the protected const (defined in SubObjectBase) // 'TagSerializationName' which was used by the // 'SerializeTag' method. It has a value of "Tag". case MySerializableClass.TagSerializationName: // Use SubObjectBase's DeserializeTag method to de-serialize the tag property. base.DeserializeTag(entry); break; default: { Debug.Assert( false, "Invalid entry in MySerializableClass de-serialization ctor" ); break; } } } } // Called to serialize the object void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { // Use the SerializeProperty static method to serialize properties. // This works for both binary and SOAP formatters. if (this.borderStyle != UIElementBorderStyle.Solid) Utils.SerializeProperty( info, "BorderStyle", this.borderStyle ); if (this.enabled != true) Utils.SerializeProperty( info, "Enabled", this.enabled ); if (this.height != 0) Utils.SerializeProperty( info, "Height", this.height ); if (!this.location.IsEmpty) Utils.SerializeProperty( info, "Location", this.location ); if (this.Key.Length > 0) Utils.SerializeProperty( info, "Key", this.Key ); // Call the SubObjectBase's SerializeTag method which will only // serialize the tag if it makes sense. base.SerializeTag(info); } } }