DataObject.SetData メソッド

定義

DataObjectにオブジェクトを追加します。

オーバーロード

名前 説明
SetData(Object)

データ形式としてオブジェクト型を使用して、指定したオブジェクトを DataObject に追加します。

SetData(String, Object)

指定した形式を使用して、指定したオブジェクトを DataObject に追加します。

SetData(Type, Object)

指定した型を形式として使用して、指定したオブジェクトを DataObject に追加します。

SetData(String, Boolean, Object)

指定した形式を使用して、指定したオブジェクトを DataObject に追加し、データを別の形式に変換できるかどうかを示します。

SetData(Object)

データ形式としてオブジェクト型を使用して、指定したオブジェクトを DataObject に追加します。

public:
 virtual void SetData(System::Object ^ data);
public virtual void SetData(object data);
abstract member SetData : obj -> unit
override this.SetData : obj -> unit
Public Overridable Sub SetData (data As Object)

パラメーター

data
Object

格納するデータ。

実装

次のコード例では、データを DataObjectに格納します。 まず、新しい DataObject が作成され、コンポーネントが格納されます。 次に、クラスを指定してデータを取得します。 結果がテキスト ボックスに表示されます。

このコードでは、 textBox1 が作成されている必要があります。

private:
   void AddMyData3()
   {
      // Creates a component to store in the data object.
      Component^ myComponent = gcnew Component;
      
      // Creates a new data object.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Adds the component to the DataObject.
      myDataObject->SetData( myComponent );
      
      // Prints whether data of the specified type is in the DataObject.
      Type^ myType = myComponent->GetType();
      if ( myDataObject->GetDataPresent( myType ) )
      {
         textBox1->Text = String::Concat( "Data of type ", myType->Name,
           " is present in the DataObject" );
      }
      else
      {
         textBox1->Text = String::Concat( "Data of type ", myType->Name,
           " is not present in the DataObject" );
      }
   }
private void AddMyData3() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Creates a new data object.
    DataObject myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject.SetData(myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    Type myType = myComponent.GetType();
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.GetType().Name + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.GetType().Name +
       " is not present in the DataObject";
 }
Private Sub AddMyData3()
    ' Creates a component to store in the data object.
    Dim myComponent As New Component()
    
    ' Creates a new data object.
    Dim myDataObject As New DataObject()
    
    ' Adds the component to the DataObject.
    myDataObject.SetData(myComponent)
    
    ' Prints whether data of the specified type is in the DataObject.
    Dim myType As Type = myComponent.GetType()
    If myDataObject.GetDataPresent(myType) Then
        textBox1.Text = "Data of type " & myType.GetType().Name & _
            " is present in the DataObject"
    Else
        textBox1.Text = "Data of type " & myType.GetType().Name & _
            " is not present in the DataObject"
    End If
End Sub

注釈

Important

信頼されていないデータを使用してこのメソッドを呼び出すことは、セキュリティ上のリスクです。 このメソッドは、信頼できるデータでのみ呼び出します。 詳細については、「すべての入力を検証する」を参照してください。

ターゲット アプリケーションの形式がわからない場合は、このメソッドを使用して複数の形式でデータを格納できます。 このメソッドを使用して格納されたデータは、取得時に互換性のある形式に変換できます。

SetData(Object) オーバーロードは、data値を、Object.GetType メソッドを呼び出して決定する形式で格納します。 data ISerializable インターフェイスを実装する場合、このオーバーロードは値をSerializable形式で格納します。

こちらもご覧ください

適用対象

SetData(String, Object)

指定した形式を使用して、指定したオブジェクトを DataObject に追加します。

public:
 virtual void SetData(System::String ^ format, System::Object ^ data);
public virtual void SetData(string format, object data);
abstract member SetData : string * obj -> unit
override this.SetData : string * obj -> unit
Public Overridable Sub SetData (format As String, data As Object)

パラメーター

format
String

データに関連付けられている形式。 定義済みの形式については、 DataFormats を参照してください。

data
Object

格納するデータ。

実装

次のコード例では、Unicode 形式を指定してデータを DataObjectに格納します。

既定では最終的な形式に互換性があるときにデータを変換するため、テキスト形式を指定してデータが取得されます。 結果がテキスト ボックスに表示されます。

このコードでは、 textBox1 が作成されている必要があります。

private:
   void AddMyData()
   {
      // Creates a new data object using a string and the text format.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Stores a string, specifying the Unicode format.
      myDataObject->SetData( DataFormats::UnicodeText, "Text string" );
      
      // Retrieves the data by specifying Text.
      textBox1->Text = myDataObject->GetData( DataFormats::Text )->GetType()->Name;
   }
private void AddMyData() {
    // Creates a new data object using a string and the text format.
    DataObject myDataObject = new DataObject();
 
    // Stores a string, specifying the Unicode format.
    myDataObject.SetData(DataFormats.UnicodeText, "Text string");
 
    // Retrieves the data by specifying Text.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).GetType().Name;
 }
Private Sub AddMyData()
    ' Creates a new data object using a string and the text format.
    Dim myDataObject As New DataObject()
    
    ' Stores a string, specifying the Unicode format.
    myDataObject.SetData(DataFormats.UnicodeText, "Text string")
    
    ' Retrieves the data by specifying Text.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).GetType().Name
End Sub

注釈

Important

信頼されていないデータを使用してこのメソッドを呼び出すことは、セキュリティ上のリスクです。 このメソッドは、信頼できるデータでのみ呼び出します。 詳細については、「すべての入力を検証する」を参照してください。

ターゲット アプリケーションの形式がわからない場合は、このメソッドを使用して複数の形式でデータを格納できます。

このメソッドを使用して格納されたデータは、取得時に互換性のある形式に変換できます。

こちらもご覧ください

適用対象

SetData(Type, Object)

指定した型を形式として使用して、指定したオブジェクトを DataObject に追加します。

public:
 virtual void SetData(Type ^ format, System::Object ^ data);
public virtual void SetData(Type format, object data);
abstract member SetData : Type * obj -> unit
override this.SetData : Type * obj -> unit
Public Overridable Sub SetData (format As Type, data As Object)

パラメーター

format
Type

データに関連付けられている形式を表す Type

data
Object

格納するデータ。

実装

次のコード例では、データ形式としてTypeを使用してデータをDataObjectに格納します。 データは、Typeを使用してGetDataを呼び出してデータ形式を指定することによって取得されます。 結果がテキスト ボックスに表示されます。

このコードでは、 textBox1 が作成されている必要があります。

private:
   void AddMyData2()
   {
      // Creates a component to store in the data object.
      Component^ myComponent = gcnew Component;
      
      // Gets the type of the component.
      Type^ myType = myComponent->GetType();
      
      // Creates a new data object.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Adds the component to the DataObject.
      myDataObject->SetData( myType, myComponent );
      
      // Prints whether data of the specified type is in the DataObject.
      if ( myDataObject->GetDataPresent( myType ) )
      {
         textBox1->Text = String::Concat( "Data of type ", myType->Name,
            " is present in the DataObject" );
      }
      else
      {
         textBox1->Text = String::Concat( "Data of type ", myType->Name,
           " is not present in the DataObject" );
      }
   }
private void AddMyData2() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Gets the type of the component.
    Type myType = myComponent.GetType();
 
    // Creates a new data object.
    DataObject myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject.SetData(myType, myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.GetType().Name + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.GetType().Name +
       " is not present in the DataObject";
 }
Private Sub AddMyData2()
    ' Creates a component to store in the data object.
    Dim myComponent As New Component()
    
    ' Gets the type of the component.
    Dim myType As Type = myComponent.GetType()
    
    ' Creates a new data object.
    Dim myDataObject As New DataObject()
    
    ' Adds the component to the DataObject.
    myDataObject.SetData(myType, myComponent)
    
    ' Prints whether data of the specified type is in the DataObject.
    If myDataObject.GetDataPresent(myType) Then
        textBox1.Text = "Data of type " & myType.GetType().Name & _
            " is present in the DataObject"
    Else
        textBox1.Text = "Data of type " & myType.GetType().Name & _
            " is not present in the DataObject"
    End If
End Sub

注釈

Important

信頼されていないデータを使用してこのメソッドを呼び出すことは、セキュリティ上のリスクです。 このメソッドは、信頼できるデータでのみ呼び出します。 詳細については、「すべての入力を検証する」を参照してください。

ターゲット アプリケーションの形式がわからない場合は、このメソッドを使用して複数の形式でデータを格納できます。

このメソッドを使用して格納されたデータは、取得時に互換性のある形式に変換できます。

こちらもご覧ください

適用対象

SetData(String, Boolean, Object)

指定した形式を使用して、指定したオブジェクトを DataObject に追加し、データを別の形式に変換できるかどうかを示します。

public:
 virtual void SetData(System::String ^ format, bool autoConvert, System::Object ^ data);
public virtual void SetData(string format, bool autoConvert, object data);
abstract member SetData : string * bool * obj -> unit
override this.SetData : string * bool * obj -> unit
Public Overridable Sub SetData (format As String, autoConvert As Boolean, data As Object)

パラメーター

format
String

データに関連付けられている形式。 定義済みの形式については、 DataFormats を参照してください。

autoConvert
Boolean

true データを別の形式に変換できるようにする場合。それ以外の場合は false

data
Object

格納するデータ。

実装

次のコード例では、データを DataObject に格納し、データをネイティブ形式でのみ取得できることを指定します。

まず、新しい DataObject が作成されます。 Unicode 形式のデータは、DataObjectに格納され、autoConvertfalseに設定されます。

次に、使用可能なデータ形式の一覧について DataObject が照会されます。 Unicode 形式のみが返されますが、Unicode データはテキストやその他の形式に変換できます。

このコードでは、 textBox1 が作成されている必要があります。

private:
   void AddMyData4()
   {
      // Creates a new data object, and assigns it the component.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Adds data to the DataObject, and specifies no format conversion.
      myDataObject->SetData( DataFormats::UnicodeText, false, "My Unicode data" );
      
      // Gets the data formats in the DataObject.
      array<String^>^ arrayOfFormats = myDataObject->GetFormats();
      
      // Prints the results.
      textBox1->Text = "The format(s) associated with the data are: \n";
      for ( int i = 0; i < arrayOfFormats->Length; i++ )
      {
         textBox1->Text = String::Concat( textBox1->Text, arrayOfFormats[ i ], "\n" );
      }
   }
private void AddMyData4() {
    // Creates a new data object, and assigns it the component.
    DataObject myDataObject = new DataObject();
 
    // Adds data to the DataObject, and specifies no format conversion.
    myDataObject.SetData(DataFormats.UnicodeText, false, "My Unicode data");
 
    // Gets the data formats in the DataObject.
    String[] arrayOfFormats = myDataObject.GetFormats();
 
    // Prints the results.
    textBox1.Text = "The format(s) associated with the data are: " + '\n';
    for(int i=0; i<arrayOfFormats.Length; i++)
       textBox1.Text += arrayOfFormats[i] + '\n';
 }
Private Sub AddMyData4()
    ' Creates a new data object, and assigns it the component.
    Dim myDataObject As New DataObject()
    
    ' Adds data to the DataObject, and specifies no format conversion.
    myDataObject.SetData(DataFormats.UnicodeText, False, "My Unicode data")
    
    ' Gets the data formats in the DataObject.
    Dim arrayOfFormats As String() = myDataObject.GetFormats()
    
    ' Prints the results.
    textBox1.Text = "The format(s) associated with the data are: " & ControlChars.Cr
    Dim i As Integer
    For i = 0 To arrayOfFormats.Length - 1
        textBox1.Text += arrayOfFormats(i) & ControlChars.Cr
    Next i
End Sub

注釈

Important

信頼されていないデータを使用してこのメソッドを呼び出すことは、セキュリティ上のリスクです。 このメソッドは、信頼できるデータでのみ呼び出します。 詳細については、「すべての入力を検証する」を参照してください。

ターゲット アプリケーションの形式がわからない場合は、このメソッドを使用して複数の形式でデータを格納できます。

こちらもご覧ください

適用対象