DataObject.SetData Metodo

Definizione

Aggiunge un oggetto all'oggetto DataObject.

Overload

SetData(Object)

Aggiunge l'oggetto specificato alla classe DataObject utilizzando il tipo di oggetto come formato dei dati.

SetData(String, Object)

Aggiunge l'oggetto specificato alla classe DataObject utilizzando il formato specificato.

SetData(Type, Object)

Aggiunge l'oggetto specificato alla classe DataObject utilizzando il tipo specificato come formato.

SetData(String, Boolean, Object)

Aggiunge l'oggetto specificato alla classe DataObject utilizzando il formato specificato e indicando se i dati possono essere convertiti in un altro formato.

SetData(Object)

Aggiunge l'oggetto specificato alla classe DataObject utilizzando il tipo di oggetto come formato dei dati.

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)

Parametri

data
Object

Dati da memorizzare.

Implementazioni

Esempio

Nell'esempio di codice seguente i dati vengono archiviati in un oggetto DataObject. Prima di tutto, viene creato un nuovo DataObject oggetto e ne viene archiviato un componente. I dati vengono quindi recuperati specificando la classe . Il risultato viene visualizzato in una casella di testo.

Questo codice richiede che textBox1 sia stato creato.

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

Commenti

Importante

La chiamata a questo metodo con dati non attendibili costituisce un rischio per la sicurezza. Chiamare questo metodo solo con dati attendibili. Per altre informazioni, vedere Convalidare tutti gli input.

Se non si conosce il formato dell'applicazione di destinazione, è possibile archiviare i dati in più formati usando questo metodo. I dati archiviati con questo metodo possono essere convertiti in un formato compatibile quando vengono recuperati.

L'overload SetData(Object) archivia il data valore in un formato determinato chiamando il Object.GetType metodo . Se data implementa l'interfaccia ISerializable , questo overload archivia anche il valore nel Serializable formato .

Vedi anche

Si applica a

SetData(String, Object)

Aggiunge l'oggetto specificato alla classe DataObject utilizzando il formato specificato.

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)

Parametri

format
String

Formato associato ai dati. Per i formati predefiniti, vedere DataFormats.

data
Object

Dati da memorizzare.

Implementazioni

Esempio

L'esempio di codice seguente archivia i dati in un oggetto DataObject, specificandone il formato come Unicode.

I dati vengono quindi recuperati specificando il formato di testo, poiché l'impostazione predefinita consiste nel convertire i dati quando il formato finale è compatibile. Il risultato viene visualizzato in una casella di testo.

Questo codice richiede che textBox1 sia stato creato.

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

Commenti

Importante

La chiamata a questo metodo con dati non attendibili costituisce un rischio per la sicurezza. Chiamare questo metodo solo con dati attendibili. Per altre informazioni, vedere Convalidare tutti gli input.

Se non si conosce il formato dell'applicazione di destinazione, è possibile archiviare i dati in più formati usando questo metodo.

I dati archiviati con questo metodo possono essere convertiti in un formato compatibile quando vengono recuperati.

Vedi anche

Si applica a

SetData(Type, Object)

Aggiunge l'oggetto specificato alla classe DataObject utilizzando il tipo specificato come formato.

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)

Parametri

format
Type

Oggetto Type che rappresenta il formato associato ai dati.

data
Object

Dati da memorizzare.

Implementazioni

Esempio

Nell'esempio di codice seguente i dati vengono archiviati in un DataObject oggetto utilizzando un Type oggetto come formato dati. I dati vengono quindi recuperati chiamando GetData utilizzando per Type specificare il formato dati. Il risultato viene visualizzato in una casella di testo.

Questo codice richiede che textBox1 sia stato creato.

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

Commenti

Importante

La chiamata a questo metodo con dati non attendibili costituisce un rischio per la sicurezza. Chiamare questo metodo solo con dati attendibili. Per altre informazioni, vedere Convalidare tutti gli input.

Se non si conosce il formato dell'applicazione di destinazione, è possibile archiviare i dati in più formati usando questo metodo.

I dati archiviati con questo metodo possono essere convertiti in un formato compatibile quando vengono recuperati.

Vedi anche

Si applica a

SetData(String, Boolean, Object)

Aggiunge l'oggetto specificato alla classe DataObject utilizzando il formato specificato e indicando se i dati possono essere convertiti in un altro 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)

Parametri

format
String

Formato associato ai dati. Per i formati predefiniti, vedere DataFormats.

autoConvert
Boolean

true per consentire la conversione dei dati in un altro formato; in caso contrario, false.

data
Object

Dati da memorizzare.

Implementazioni

Esempio

L'esempio di codice seguente archivia i dati in e DataObject specifica che i dati possono essere recuperati solo nel formato nativo.

Prima di tutto, viene creato un nuovo DataObject oggetto . I dati nel formato Unicode vengono archiviati in DataObject, con autoConvert impostato su false.

DataObject Viene quindi eseguita una query per l'elenco dei formati di dati disponibili. Viene restituito solo il formato Unicode, anche se i dati Unicode possono essere convertiti in testo e in altri formati.

Questo codice richiede che textBox1 sia stato creato.

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

Commenti

Importante

La chiamata a questo metodo con dati non attendibili costituisce un rischio per la sicurezza. Chiamare questo metodo solo con dati attendibili. Per altre informazioni, vedere Convalidare tutti gli input.

Se non si conosce il formato dell'applicazione di destinazione, è possibile archiviare i dati in più formati usando questo metodo.

Vedi anche

Si applica a