SerializationInfo.GetValue(String, Type) Método
Definição
Recupera um valor do repositório do SerializationInfo.Retrieves a value from the SerializationInfo store.
public:
System::Object ^ GetValue(System::String ^ name, Type ^ type);
public object? GetValue (string name, Type type);
public object GetValue (string name, Type type);
member this.GetValue : string * Type -> obj
Public Function GetValue (name As String, type As Type) As Object
Parâmetros
- name
- String
O nome associado ao valor a ser recuperado.The name associated with the value to retrieve.
- type
- Type
O Type do valor a ser recuperado.The Type of the value to retrieve. Se o valor armazenado não puder ser convertido nesse tipo, o sistema gerará um InvalidCastException.If the stored value cannot be converted to this type, the system will throw a InvalidCastException.
Retornos
O objeto do Type especificado associado ao name.The object of the specified Type associated with name.
Exceções
name ou type é null.name or type is null.
O valor associado a name não pode ser convertido em type.The value associated with name cannot be converted to type.
Um elemento com o nome especificado não foi encontrado na instância atual.An element with the specified name is not found in the current instance.
Exemplos
O exemplo de código a seguir demonstra o uso do GetValue método:The following code example demonstrates the use of the GetValue method:
// A serializable LinkedList example. For the full LinkedList implementation
// see the Serialization sample.
[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.
[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.
<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
Comentários
Se os dados armazenados no SerializationInfo forem do tipo solicitado (ou uma de suas classes derivadas), esse valor será retornado diretamente.If the data stored in the SerializationInfo is of the type requested (or one of its derived classes), that value is returned directly. Caso contrário, IFormatterConverter.Convert é chamado para convertê-lo para o tipo apropriado.Otherwise, IFormatterConverter.Convert is called to convert it to the appropriate type.
O valor retornado pelo GetValue método sempre pode ser convertido com segurança para o tipo especificado no type parâmetro.The value returned by the GetValue method can always be safely cast to the type specified in the type parameter.