DataObject.SetData Methode

Definition

Fügt der DataObject ein Objekt hinzu.

Überlädt

SetData(Object)

Fügt das angegebene Objekt dem DataObject mit dem Objekttyp als Datenformat hinzu.

SetData(String, Object)

Fügt dem DataObject das angegebene Objekt mit dem angegebenen Format hinzu.

SetData(Type, Object)

Fügt dem DataObject das angegebene Objekt mit dem angegebenen Typ als Format hinzu.

SetData(String, Boolean, Object)

Fügt dem DataObject das angegebene Objekt mit dem angegebenen Format hinzu, wobei angegeben wird, ob die Daten in ein anderes Format konvertiert werden können.

SetData(Object)

Fügt das angegebene Objekt dem DataObject mit dem Objekttyp als Datenformat hinzu.

public:
 virtual void SetData(System::Object ^ data);
public virtual void SetData (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)

Parameter

data
Object

Die zu speichernden Daten.

Implementiert

Beispiele

Im folgenden Codebeispiel werden Daten in einem DataObjectgespeichert. Zunächst wird ein neues DataObject erstellt und eine Komponente darin gespeichert. Anschließend werden die Daten durch Angabe der -Klasse abgerufen. Das Ergebnis wird in einem Textfeld angezeigt.

Dieser Code erfordert, dass textBox1 erstellt wurde.

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

Hinweise

Wichtig

Das Aufrufen dieser Methode mit nicht vertrauenswürdigen Daten stellt ein Sicherheitsrisiko dar. Rufen Sie diese Methode nur mit vertrauenswürdigen Daten auf. Weitere Informationen finden Sie unter Überprüfen aller Eingaben.

Wenn Sie das Format der Zielanwendung nicht kennen, können Sie Mit dieser Methode Daten in mehreren Formaten speichern. Mit dieser Methode gespeicherte Daten können beim Abrufen in ein kompatibles Format konvertiert werden.

Die SetData(Object) Überladung speichert den data Wert in einem Format, das sie durch Aufrufen der Object.GetType -Methode bestimmt. Wenn data die ISerializable Schnittstelle implementiert wird, speichert diese Überladung auch den Wert im Serializable Format.

Weitere Informationen

Gilt für:

SetData(String, Object)

Fügt dem DataObject das angegebene Objekt mit dem angegebenen Format hinzu.

public:
 virtual void SetData(System::String ^ format, System::Object ^ data);
public virtual void SetData (string format, 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)

Parameter

format
String

Das den Daten zugeordnete Format. Vordefinierte Formate finden Sie unter DataFormats.

data
Object

Die zu speichernden Daten.

Implementiert

Beispiele

Im folgenden Codebeispiel werden Daten in einem DataObjectgespeichert, das das Format als Unicode angibt.

Anschließend werden die Daten durch Angabe des Textformats abgerufen, da die Standardeinstellung darin besteht, die Daten zu konvertieren, wenn das endgültige Format kompatibel ist. Das Ergebnis wird in einem Textfeld angezeigt.

Dieser Code erfordert, dass textBox1 erstellt wurde.

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

Hinweise

Wichtig

Das Aufrufen dieser Methode mit nicht vertrauenswürdigen Daten stellt ein Sicherheitsrisiko dar. Rufen Sie diese Methode nur mit vertrauenswürdigen Daten auf. Weitere Informationen finden Sie unter Überprüfen aller Eingaben.

Wenn Sie das Format der Zielanwendung nicht kennen, können Sie Mit dieser Methode Daten in mehreren Formaten speichern.

Mit dieser Methode gespeicherte Daten können beim Abrufen in ein kompatibles Format konvertiert werden.

Weitere Informationen

Gilt für:

SetData(Type, Object)

Fügt dem DataObject das angegebene Objekt mit dem angegebenen Typ als Format hinzu.

public:
 virtual void SetData(Type ^ format, System::Object ^ data);
public virtual void SetData (Type format, 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)

Parameter

format
Type

Ein Type, der das den Daten zugeordnete Format darstellt.

data
Object

Die zu speichernden Daten.

Implementiert

Beispiele

Im folgenden Codebeispiel werden Daten in einem DataObject mit einem Type als Datenformat gespeichert. Die Daten werden dann mithilfe von abgerufen, um GetDataType das Datenformat anzugeben. Das Ergebnis wird in einem Textfeld angezeigt.

Dieser Code erfordert, dass textBox1 erstellt wurde.

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

Hinweise

Wichtig

Das Aufrufen dieser Methode mit nicht vertrauenswürdigen Daten stellt ein Sicherheitsrisiko dar. Rufen Sie diese Methode nur mit vertrauenswürdigen Daten auf. Weitere Informationen finden Sie unter Überprüfen aller Eingaben.

Wenn Sie das Format der Zielanwendung nicht kennen, können Sie Mit dieser Methode Daten in mehreren Formaten speichern.

Mit dieser Methode gespeicherte Daten können beim Abrufen in ein kompatibles Format konvertiert werden.

Weitere Informationen

Gilt für:

SetData(String, Boolean, Object)

Fügt dem DataObject das angegebene Objekt mit dem angegebenen Format hinzu, wobei angegeben wird, ob die Daten in ein anderes Format konvertiert werden können.

public:
 virtual void SetData(System::String ^ format, bool autoConvert, System::Object ^ data);
public virtual void SetData (string format, bool autoConvert, 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)

Parameter

format
String

Das den Daten zugeordnete Format. Vordefinierte Formate finden Sie unter DataFormats.

autoConvert
Boolean

true, um die Konvertierung der Daten in ein anderes Format zuzulassen, andernfalls false.

data
Object

Die zu speichernden Daten.

Implementiert

Beispiele

Im folgenden Codebeispiel werden Daten in einem DataObject gespeichert und angegeben, dass die Daten nur im nativen Format abgerufen werden können.

Zunächst wird ein neues DataObject erstellt. Daten im Unicode-Format werden in DataObjectgespeichert, wobei autoConvert auf falsefestgelegt ist.

Anschließend wird nach DataObject der Liste der verfügbaren Datenformate abgefragt. Es wird nur das Unicode-Format zurückgegeben, obwohl Unicode-Daten in Text und andere Formate konvertiert werden können.

Dieser Code erfordert, dass textBox1 erstellt wurde.

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

Hinweise

Wichtig

Das Aufrufen dieser Methode mit nicht vertrauenswürdigen Daten stellt ein Sicherheitsrisiko dar. Rufen Sie diese Methode nur mit vertrauenswürdigen Daten auf. Weitere Informationen finden Sie unter Überprüfen aller Eingaben.

Wenn Sie das Format der Zielanwendung nicht kennen, können Sie Mit dieser Methode Daten in mehreren Formaten speichern.

Weitere Informationen

Gilt für: