DataObject.SetData Método

Definición

Agrega un objeto a DataObject.

Sobrecargas

SetData(Object)

Agrega el objeto especificado a DataObject utilizando el tipo de objeto como formato de datos.

SetData(String, Object)

Agrega el objeto determinado a DataObject utilizando el formato especificado.

SetData(Type, Object)

Agrega el objeto especificado a DataObject utilizando como formato el tipo del objeto.

SetData(String, Boolean, Object)

Agrega el objeto determinado a DataObject utilizando el formato especificado e indicando si los datos se pueden convertir a otro formato.

SetData(Object)

Agrega el objeto especificado a DataObject utilizando el tipo de objeto como formato de datos.

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)

Parámetros

data
Object

Datos que se van a almacenar.

Implementaciones

Ejemplos

En el ejemplo de código siguiente se almacenan los datos de .DataObject En primer lugar, se crea un nuevo DataObject y se almacena un componente en él. A continuación, los datos se recuperan especificando la clase . El resultado se muestra en un cuadro de texto.

Este código requiere que textBox1 se haya creado.

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

Comentarios

Importante

Llamar a este método con datos que no son de confianza supone un riesgo de seguridad. Llame a este método solo con datos de confianza. Para obtener más información, vea Validar todas las entradas.

Si no conoce el formato de la aplicación de destino, puede almacenar datos en varios formatos mediante este método. Los datos almacenados con este método se pueden convertir a un formato compatible cuando se recuperan.

La SetData(Object) sobrecarga almacena el data valor en un formato que determina llamando al Object.GetType método . Si data implementa la ISerializable interfaz , esta sobrecarga también almacena el valor en el Serializable formato .

Consulte también

Se aplica a

SetData(String, Object)

Agrega el objeto determinado a DataObject utilizando el formato especificado.

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)

Parámetros

format
String

Formato asociado a los datos. Vea DataFormats para obtener los formatos predefinidos.

data
Object

Datos que se van a almacenar.

Implementaciones

Ejemplos

En el ejemplo de código siguiente se almacenan los datos de , DataObjectespecificando su formato como Unicode.

A continuación, los datos se recuperan especificando el formato de texto, ya que el valor predeterminado es convertir los datos cuando el formato final es compatible. El resultado se muestra en un cuadro de texto.

Este código requiere que textBox1 se haya creado.

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

Comentarios

Importante

Llamar a este método con datos que no son de confianza supone un riesgo de seguridad. Llame a este método solo con datos de confianza. Para obtener más información, vea Validar todas las entradas.

Si no conoce el formato de la aplicación de destino, puede almacenar datos en varios formatos mediante este método.

Los datos almacenados con este método se pueden convertir a un formato compatible cuando se recuperan.

Consulte también

Se aplica a

SetData(Type, Object)

Agrega el objeto especificado a DataObject utilizando como formato el tipo del objeto.

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)

Parámetros

format
Type

Type que representa el formato asociado a los datos.

data
Object

Datos que se van a almacenar.

Implementaciones

Ejemplos

En el ejemplo de código siguiente se almacenan los datos de mediante DataObject un Type como formato de datos. A continuación, los datos se recuperan mediante una Type llamada a GetData mediante para especificar el formato de datos. El resultado se muestra en un cuadro de texto.

Este código requiere que textBox1 se haya creado.

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

Comentarios

Importante

Llamar a este método con datos que no son de confianza supone un riesgo de seguridad. Llame a este método solo con datos de confianza. Para obtener más información, vea Validar todas las entradas.

Si no conoce el formato de la aplicación de destino, puede almacenar datos en varios formatos mediante este método.

Los datos almacenados con este método se pueden convertir a un formato compatible cuando se recuperan.

Consulte también

Se aplica a

SetData(String, Boolean, Object)

Agrega el objeto determinado a DataObject utilizando el formato especificado e indicando si los datos se pueden convertir a otro formato.

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)

Parámetros

format
String

Formato asociado a los datos. Vea DataFormats para obtener los formatos predefinidos.

autoConvert
Boolean

true para permitir la conversión de los datos a otros formatos; en cualquier otro caso, false.

data
Object

Datos que se van a almacenar.

Implementaciones

Ejemplos

En el ejemplo de código siguiente se almacenan datos en DataObject y se especifica que los datos solo se pueden recuperar en su formato nativo.

En primer lugar, se crea un nuevo DataObject . Los datos en el formato Unicode se almacenan en , con autoConvert establecido falseen DataObject.

A continuación, DataObject se consulta la lista de formatos de datos disponibles. Solo se devuelve el formato Unicode, aunque los datos Unicode se pueden convertir en texto y otros formatos.

Este código requiere que textBox1 se haya creado.

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

Comentarios

Importante

Llamar a este método con datos que no son de confianza supone un riesgo de seguridad. Llame a este método solo con datos de confianza. Para obtener más información, vea Validar todas las entradas.

Si no conoce el formato de la aplicación de destino, puede almacenar datos en varios formatos mediante este método.

Consulte también

Se aplica a