SerializationInfo.GetValue 方法

SerializationInfo 存储区中检索一个值。

**命名空间:**System.Runtime.Serialization
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Function GetValue ( _
    name As String, _
    type As Type _
) As Object
用法
Dim instance As SerializationInfo
Dim name As String
Dim type As Type
Dim returnValue As Object

returnValue = instance.GetValue(name, type)
public Object GetValue (
    string name,
    Type type
)
public:
Object^ GetValue (
    String^ name, 
    Type^ type
)
public Object GetValue (
    String name, 
    Type type
)
public function GetValue (
    name : String, 
    type : Type
) : Object

参数

  • name
    与要检索的值关联的名称。

返回值

与 name 关联的指定 Type 的对象。

异常

异常类型 条件

ArgumentNullException

name 或 type 为 空引用(在 Visual Basic 中为 Nothing)。

InvalidCastException

与 name 关联的值不能转换为 type。

SerializationException

当前实例中没有找到具有指定名称的元素。

备注

如果存储在 SerializationInfo 中的数据属于所请求的类型(或者是其派生类之一),则直接返回该值。否则,会调用 IFormatterConverter.Convert 来将其转换为适当的类型。

GetValue 方法返回的值始终可以安全地转换为在 type 参数中指定的类型。

示例

下面的代码示例演示 GetValue 方法的用法:

' A serializable LinkedList example.  For the full LinkedList implementation
' see the Serialization sample in the .NET Framework SDK.
<Serializable()> Class LinkedList
    Implements ISerializable

    Public Shared Sub Main()
    End Sub

    Private m_head As Node = Nothing
    Private m_tail As Node = Nothing    
    
    ' Serializes the object.
    Public Sub GetObjectData(info As SerializationInfo, _
    context As StreamingContext) Implements ISerializable.GetObjectData
    
        ' Stores the m_head and m_tail references in the SerializationInfo info.
        info.AddValue("head", m_head, m_head.GetType())
        info.AddValue("tail", m_tail, m_tail.GetType())
    End Sub
    
    
    ' Constructor that is called automatically during deserialization.
    ' Reconstructs the object from the information in SerializationInfo info.
    Private Sub New(info As SerializationInfo, context As StreamingContext)
        Dim temp As New Node(0)
        ' Retrieves the values of Type temp.GetType() from SerializationInfo info.
        m_head = CType(info.GetValue("head", temp.GetType()), Node)
        m_tail = CType(info.GetValue("tail", temp.GetType()), Node)
    End Sub
End Class
// A serializable LinkedList example.  For the full LinkedList implementation
// see the Serialization sample in the .NET Framework SDK.
[Serializable()]
class LinkedList: ISerializable {

   public static void Main() {}

   Node m_head = null;
   Node m_tail = null;
   
   // Serializes the object.
   [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter=true)]
   public void GetObjectData(SerializationInfo info, StreamingContext context){
      // Stores the m_head and m_tail references in the SerializationInfo info.
      info.AddValue("head", m_head, m_head.GetType());
      info.AddValue("tail", m_tail, m_tail.GetType());
   }
   
   // Constructor that is called automatically during deserialization.
   // Reconstructs the object from the information in SerializationInfo info
   [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter=true)]
   private LinkedList(SerializationInfo info, StreamingContext context)
   {      
      Node temp = new Node(0);
      // Retrieves the values of Type temp.GetType() from SerializationInfo info
      m_head = (Node)info.GetValue("head", temp.GetType());
      m_tail = (Node)info.GetValue("tail", temp.GetType());
   }
}
// A serializable LinkedList example.  For the full LinkedList implementation
// see the Serialization sample in the .NET Framework SDK.
[Serializable]
ref class LinkedList: public ISerializable
{
private:
   Node^ m_head;
   Node^ m_tail;

   // Serializes the object.
public:
   virtual void GetObjectData( SerializationInfo^ info, StreamingContext /*context*/ )
   {
      // Stores the m_head and m_tail references in the SerializationInfo info.
      info->AddValue( "head", m_head, m_head->GetType() );
      info->AddValue( "tail", m_tail, m_tail->GetType() );
   }

   // Constructor that is called automatically during deserialization.
private:
   // Reconstructs the object from the information in SerializationInfo info
   LinkedList( SerializationInfo^ info, StreamingContext /*context*/ )
   {
      Node^ temp = gcnew Node( 0 );
      // Retrieves the values of Type temp.GetType() from SerializationInfo info
      m_head = dynamic_cast<Node^>(info->GetValue( "head", temp->GetType() ));
      m_tail = dynamic_cast<Node^>(info->GetValue( "tail", temp->GetType() ));
   }
};
// A serializable LinkedList example.  For the full LinkedList implementation
// see the Serialization sample in the.NET Framework SDK.

/** @attribute Serializable()
 */

class LinkedList implements ISerializable
{
    public static void main(String[] args)
    {
    } //main
 
    private Node m_head = null;
    private Node m_tail = null;

    // Serializes the object.
   /** @attribute SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter=true)
    */
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        // Stores the m_head and m_tail references in the SerializationInfo 
        // info.
        info.AddValue("head", m_head, m_head.GetType());
        info.AddValue("tail", m_tail, m_tail.GetType());
    } // GetObjectData

    // Constructor that is called automatically during deserialization.
    // Reconstructs the object from the information in SerializationInfo info
   /** @attribute SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter=true)
    */
    private LinkedList(SerializationInfo info, StreamingContext context)
    {
        Node temp = new Node(0);

        // Retrieves the values of Type temp.GetType() from SerializationInfo 
        // info
        m_head = ((Node)(info.GetValue("head", temp.GetType())));
        m_tail = ((Node)(info.GetValue("tail", temp.GetType())));
    } //LinkedList
} //LinkedList

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

SerializationInfo 类
SerializationInfo 成员
System.Runtime.Serialization 命名空间