DataObject.SetData Método

Definición

Agrega un objeto a .DataObject

Sobrecargas

Nombre Description
SetData(Object)

Agrega el objeto especificado al DataObject uso del tipo de objeto como formato de datos.

SetData(String, Object)

Agrega el objeto especificado al DataObject utilizando el formato especificado.

SetData(Type, Object)

Agrega el objeto especificado al DataObject utilizando el tipo especificado como formato.

SetData(String, Boolean, Object)

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

SetData(Object)

Agrega el objeto especificado al DataObject uso del tipo de objeto como formato de datos.

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)

Parámetros

data
Object

Datos que se van a almacenar.

Implementaciones

Ejemplos

En el ejemplo de código siguiente se almacenan los datos de un 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 es 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 mediante este método se pueden convertir a un formato compatible cuando se recupera.

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 especificado al DataObject utilizando el formato especificado.

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)

Parámetros

format
String

Formato asociado a los datos. Consulte DataFormats para conocer 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 sea 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 es 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 mediante este método se pueden convertir a un formato compatible cuando se recupera.

Consulte también

Se aplica a

SetData(Type, Object)

Agrega el objeto especificado al DataObject utilizando el tipo especificado como formato.

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)

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 un DataObject objeto utilizando 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 es 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 mediante este método se pueden convertir a un formato compatible cuando se recupera.

Consulte también

Se aplica a

SetData(String, Boolean, Object)

Agrega el objeto especificado al DataObject utilizando el formato especificado e indica 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);
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. Consulte DataFormats para conocer los formatos predefinidos.

autoConvert
Boolean

true para permitir que los datos se conviertan a otro formato; de lo contrario, false.

data
Object

Datos que se van a almacenar.

Implementaciones

Ejemplos

En el ejemplo de código siguiente se almacenan los datos de 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 del 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 es 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