SerializationInfo.GetValue(String, Type) Yöntem

Tanım

Depodan SerializationInfo bir değer alır.

C#
public object? GetValue (string name, Type type);
C#
public object GetValue (string name, Type type);

Parametreler

name
String

Alınacak değerle ilişkili ad.

type
Type

Type Alınacak değerin değeri. Depolanan değer bu türe dönüştürülemezse, sistem bir InvalidCastExceptionoluşturur.

Döndürülenler

ile nameilişkilendirilmiş belirtilen Type öğesinin nesnesi.

Özel durumlar

name veya type şeklindedir null.

ile name ilişkilendirilmiş değer olarak typedönüştürülemez.

Belirtilen ada sahip bir öğe geçerli örnekte bulunamadı.

Örnekler

Aşağıdaki kod örneği yönteminin GetValue kullanımını gösterir:

C#
// 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.
   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
   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çıklamalar

içinde SerializationInfo depolanan veriler istenen türdeyse (veya türetilmiş sınıflarından biriyse), bu değer doğrudan döndürülür. Aksi takdirde, IFormatterConverter.Convert uygun türe dönüştürmek için çağrılır.

yöntemi tarafından GetValue döndürülen değer her zaman parametresinde type belirtilen türe güvenli bir şekilde yayınlanabilir.

Şunlara uygulanır

Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1