DataObject.SetData Metoda

Definicja

Dodaje obiekt do obiektu DataObject.

Przeciążenia

SetData(Object)

Dodaje określony obiekt do DataObject obiektu przy użyciu typu obiektu jako format danych.

SetData(String, Object)

Dodaje określony obiekt do DataObject obiektu przy użyciu określonego formatu.

SetData(Type, Object)

Dodaje określony obiekt do DataObject obiektu przy użyciu określonego typu jako formatu.

SetData(String, Boolean, Object)

Dodaje określony obiekt do DataObject obiektu przy użyciu określonego formatu i wskazuje, czy dane można przekonwertować na inny format.

SetData(Object)

Dodaje określony obiekt do DataObject obiektu przy użyciu typu obiektu jako format danych.

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)

Parametry

data
Object

Dane do przechowywania.

Implementuje

Przykłady

Poniższy przykład kodu przechowuje dane w obiekcie DataObject. Najpierw tworzony jest nowy DataObject składnik i jest w nim przechowywany. Następnie dane są pobierane przez określenie klasy. Wynik jest wyświetlany w polu tekstowym.

Ten kod wymaga textBox1 utworzenia.

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

Uwagi

Ważne

Wywoływanie tej metody z niezaufanymi danymi jest zagrożeniem bezpieczeństwa. Wywołaj tę metodę tylko z zaufanymi danymi. Aby uzyskać więcej informacji, zobacz Validate All Inputs (Weryfikowanie wszystkich danych wejściowych).

Jeśli nie znasz formatu aplikacji docelowej, możesz przechowywać dane w wielu formatach przy użyciu tej metody. Dane przechowywane przy użyciu tej metody można przekonwertować na zgodny format podczas pobierania.

Przeciążenie SetData(Object) przechowuje data wartość w formacie, który określa przez wywołanie Object.GetType metody . Jeśli data implementuje ISerializable interfejs, to przeciążenie przechowuje również wartość w Serializable formacie.

Zobacz też

Dotyczy

SetData(String, Object)

Dodaje określony obiekt do DataObject obiektu przy użyciu określonego formatu.

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)

Parametry

format
String

Format skojarzony z danymi. Zobacz DataFormats , aby zapoznać się ze wstępnie zdefiniowanymi formatami.

data
Object

Dane do przechowywania.

Implementuje

Przykłady

Poniższy przykład kodu przechowuje dane w obiekcie DataObject, określając jego format jako Unicode.

Następnie dane są pobierane przez określenie formatu tekstowego, ponieważ domyślnie jest konwertowanie danych, gdy format końcowy jest zgodny. Wynik jest wyświetlany w polu tekstowym.

Ten kod wymaga textBox1 utworzenia.

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

Uwagi

Ważne

Wywoływanie tej metody z niezaufanymi danymi jest zagrożeniem bezpieczeństwa. Wywołaj tę metodę tylko z zaufanymi danymi. Aby uzyskać więcej informacji, zobacz Validate All Inputs (Weryfikowanie wszystkich danych wejściowych).

Jeśli nie znasz formatu aplikacji docelowej, możesz przechowywać dane w wielu formatach przy użyciu tej metody.

Dane przechowywane przy użyciu tej metody można przekonwertować na zgodny format podczas pobierania.

Zobacz też

Dotyczy

SetData(Type, Object)

Dodaje określony obiekt do DataObject obiektu przy użyciu określonego typu jako formatu.

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)

Parametry

format
Type

Reprezentujący Type format skojarzony z danymi.

data
Object

Dane do przechowywania.

Implementuje

Przykłady

Poniższy przykładowy kod przechowuje dane w formacie DataObject przy użyciu elementu Type jako formatu danych. Następnie dane są pobierane przez wywołanie GetData metody w Type celu określenia formatu danych. Wynik jest wyświetlany w polu tekstowym.

Ten kod wymaga textBox1 utworzenia.

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

Uwagi

Ważne

Wywoływanie tej metody z niezaufanymi danymi jest zagrożeniem bezpieczeństwa. Wywołaj tę metodę tylko z zaufanymi danymi. Aby uzyskać więcej informacji, zobacz Validate All Inputs (Weryfikowanie wszystkich danych wejściowych).

Jeśli nie znasz formatu aplikacji docelowej, możesz przechowywać dane w wielu formatach przy użyciu tej metody.

Dane przechowywane przy użyciu tej metody można przekonwertować na zgodny format podczas pobierania.

Zobacz też

Dotyczy

SetData(String, Boolean, Object)

Dodaje określony obiekt do DataObject obiektu przy użyciu określonego formatu i wskazuje, czy dane można przekonwertować na inny format.

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)

Parametry

format
String

Format skojarzony z danymi. Zobacz DataFormats , aby zapoznać się ze wstępnie zdefiniowanymi formatami.

autoConvert
Boolean

true aby umożliwić konwersję danych na inny format; w przeciwnym razie , false.

data
Object

Dane do przechowywania.

Implementuje

Przykłady

Poniższy przykład kodu przechowuje dane w obiekcie DataObject i określa, że dane mogą być pobierane tylko w formacie natywnym.

Najpierw jest tworzony nowy DataObject . Dane w formacie Unicode są przechowywane w obiekcie DataObjectz ustawioną wartością falseautoConvert .

DataObject Następnie zapytanie dotyczy listy dostępnych formatów danych. Zwracany jest tylko format Unicode, chociaż dane Unicode można przekonwertować na tekst i inne formaty.

Ten kod wymaga textBox1 utworzenia.

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

Uwagi

Ważne

Wywoływanie tej metody z niezaufanymi danymi jest zagrożeniem bezpieczeństwa. Wywołaj tę metodę tylko z zaufanymi danymi. Aby uzyskać więcej informacji, zobacz Validate All Inputs (Weryfikowanie wszystkich danych wejściowych).

Jeśli nie znasz formatu aplikacji docelowej, możesz przechowywać dane w wielu formatach przy użyciu tej metody.

Zobacz też

Dotyczy