SerializationInfo.GetValue(String, Type) メソッド

定義

SerializationInfo ストアから値を取得します。

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

パラメーター

name
String

取得する値に関連付けられた名前。

type
Type

取得する値の Type。 格納された値がこの型に変換できない場合は、InvalidCastException がスローされます。

戻り値

name に関連付けられた、指定した Type のオブジェクト。

例外

name または typenull です。

name に関連付けられた値を type に変換できません。

指定した名前の要素が、現在のインスタンス内に見つかりません。

次のコード例は、 メソッドの使用方法を GetValue 示しています。

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());
   }
}

注釈

SerializationInfo 格納されているデータが、要求された型 (またはその派生クラスのいずれか) の場合、その値が直接返されます。 それ以外の場合は、 IFormatterConverter.Convert が呼び出され、適切な型に変換されます。

メソッドによって GetValue 返される値は、常に パラメーターで指定された型に安全に type キャストできます。

適用対象

製品 バージョン
.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