ObjectStateFormatter.Deserialize 方法
定义
从对象状态图的序列化形式对该对象状态图进行反序列化。Deserializes an object state graph from serialized form.
重载
| Deserialize(Stream) |
从包含在指定的 Stream 对象中的对象状态图的二进制序列化形式对该对象状态图进行反序列化。Deserializes an object state graph from its binary-serialized form that is contained in the specified Stream object. |
| Deserialize(String) |
从对象状态图的序列化 Base64 编码字符串形式对该对象状态图进行反序列化。Deserializes an object state graph from its serialized base64-encoded string form. |
注解
重要
使用不受信任的数据调用此方法存在安全风险。Calling this method with untrusted data is a security risk. 仅使用受信任的数据调用此方法。Call this method only with trusted data. 有关详细信息,请参阅数据验证。For more information, see Data Validation.
Deserialize(Stream)
public:
System::Object ^ Deserialize(System::IO::Stream ^ inputStream);
public object Deserialize (System.IO.Stream inputStream);
member this.Deserialize : System.IO.Stream -> obj
Public Function Deserialize (inputStream As Stream) As Object
参数
- inputStream
- Stream
一个 Stream,ObjectStateFormatter 将其反序列化为一个初始化的 object。A Stream that the ObjectStateFormatter deserializes into an initialized object.
返回
表示反序列化的对象状态图的对象。An object that represents a deserialized object state graph.
例外
指定的 inputStream 为 null。The specified inputStream is null.
在 Stream 的反序列化期间发生异常。An exception occurs during deserialization of the Stream. 异常消息被追加到 ArgumentException 的消息。The exception message is appended to the message of the ArgumentException.
示例
下面的代码示例演示如何将派生自类的类 PageStatePersister 初始化该 ViewState 集合。The following code example demonstrates how a class that derives from the PageStatePersister class initializes the ViewState collection. 在此示例中,已将 ViewState 集合分配给 First 对象的字段 Pair ,并使用类将其序列化到文件中 ObjectStateFormatter 。In this example, the ViewState collection has been assigned to the First field of a Pair object, and serialized to a file using the ObjectStateFormatter class. Load调用方法时, Deserialize(Stream) 方法用于从文件反序列化视图状态,并 ViewState 初始化属性。When the Load method is called, the Deserialize(Stream) method is used to deserialize view state from the file, and the ViewState property is initialized. 此代码示例是为类提供的更大示例的一部分 PageStatePersister 。This code example is part of a larger example provided for the PageStatePersister class.
//
// Load ViewState and ControlState.
//
public override void Load()
{
Stream stateStream = GetSecureStream();
// Read the state string, using the StateFormatter.
StreamReader reader = new StreamReader(stateStream);
IStateFormatter formatter = this.StateFormatter;
string fileContents = reader.ReadToEnd();
// Deserilize returns the Pair object that is serialized in
// the Save method.
Pair statePair = (Pair)formatter.Deserialize(fileContents);
ViewState = statePair.First;
ControlState = statePair.Second;
reader.Close();
stateStream.Close();
}
'
' Load ViewState and ControlState.
'
Public Overrides Sub Load()
Dim stateStream As Stream
stateStream = GetSecureStream()
' Read the state string, using the StateFormatter.
Dim reader As New StreamReader(stateStream)
Dim serializedStatePair As String
serializedStatePair = reader.ReadToEnd
Dim statePair As Pair
Dim formatter As IStateFormatter
formatter = Me.StateFormatter
' Deserilize returns the Pair object that is serialized in
' the Save method.
statePair = CType(formatter.Deserialize(serializedStatePair), Pair)
ViewState = statePair.First
ControlState = statePair.Second
reader.Close()
stateStream.Close()
End Sub
注解
Serialize可以通过方法反序列化用方法序列化的任何对象状态图 Deserialize 。Any object state graph that is serialized with the Serialize method can be deserialized with the Deserialize method. Deserialize(Stream)方法用于还原中存储的对象状态图 Stream ,如 FileStream 。The Deserialize(Stream) method is used to restore an object state graph stored in a Stream, such as a FileStream.
重要
使用不受信任的数据调用此方法存在安全风险。Calling this method with untrusted data is a security risk. 仅使用受信任的数据调用此方法。Call this method only with trusted data. 有关详细信息,请参阅数据验证。For more information, see Data Validation.
适用于
Deserialize(String)
从对象状态图的序列化 Base64 编码字符串形式对该对象状态图进行反序列化。Deserializes an object state graph from its serialized base64-encoded string form.
public:
System::Object ^ Deserialize(System::String ^ inputString);
public object Deserialize (string inputString);
member this.Deserialize : string -> obj
Public Function Deserialize (inputString As String) As Object
参数
- inputString
- String
ObjectStateFormatter 会将其反序列化为初始化对象的字符串。A string that the ObjectStateFormatter deserializes into an initialized object.
返回
表示反序列化的对象状态图的对象。An object that represents a deserialized object state graph.
例外
序列化的数据无效。The serialized data is invalid.
当对视图状态进行序列化失败时执行的计算机验证代码 (MAC) 验证检查。The machine authentication code (MAC) validation check that is performed when deserializing view state fails.
示例
下面的代码示例演示如何实现一个方法,该方法可对 base64 编码的字符串进行反序列化,并返回 ICollection 属性设置的集合。The following code example demonstrates how to implement a method that deserializes a base64-encoded string and returns an ICollection collection of property settings. 此代码示例依赖于已与类序列化的属性设置 ObjectStateFormatter ,如方法中所示 Serialize(Object) 。This code example relies on the property settings having been serialized with the ObjectStateFormatter class, as shown in the Serialize(Object) method.
private ICollection LoadControlProperties (string serializedProperties) {
ICollection controlProperties = null;
// Create an ObjectStateFormatter to deserialize the properties.
ObjectStateFormatter formatter = new ObjectStateFormatter();
// Call the Deserialize method.
controlProperties = (ArrayList) formatter.Deserialize(serializedProperties);
return controlProperties;
}
Private Function LoadControlProperties(serializedProperties As String) As ICollection
Dim controlProperties As ICollection = Nothing
' Create an ObjectStateFormatter to deserialize the properties.
Dim formatter As New ObjectStateFormatter()
' Call the Deserialize method.
controlProperties = CType(formatter.Deserialize(serializedProperties), ArrayList)
Return controlProperties
End Function 'LoadControlProperties
注解
Serialize可以通过方法反序列化用方法序列化的任何对象状态图 Deserialize 。Any object state graph that is serialized with the Serialize method can be deserialized with the Deserialize method. Deserialize(String)方法用于还原以 base64 编码的字符串形式存储的对象状态图。The Deserialize(String) method is used to restore an object state graph stored in base64-encoded string form.
重要
使用不受信任的数据调用此方法存在安全风险。Calling this method with untrusted data is a security risk. 仅使用受信任的数据调用此方法。Call this method only with trusted data. 有关详细信息,请参阅数据验证。For more information, see Data Validation.