DataObject.SetData 方法

定義

將物件加入 DataObject

多載

SetData(Object)

使用物件類型作為資料格式,將指定的物件新增至 DataObject

SetData(String, Object)

使用指定的格式,將指定的物件新增至 DataObject

SetData(Type, Object)

使用指定的類型作為格式,將指定的物件新增至 DataObject

SetData(String, Boolean, Object)

使用指定的格式,將指定的物件新增至 DataObject,並指出該資料是否可以轉換成另一種格式。

SetData(Object)

使用物件類型作為資料格式,將指定的物件新增至 DataObject

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)

參數

data
Object

要儲存的資料。

實作

範例

下列程式碼範例會將資料儲存在 DataObject 中。 首先,會建立新的 DataObject ,並將元件儲存在其中。 然後,藉由指定 類別來擷取資料。 結果會顯示在文字方塊中。

此程式碼需要 textBox1 已建立。

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

備註

重要

使用不信任的資料呼叫此方法,會造成安全性上的風險。 呼叫此方法時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入

如果您不知道目標應用程式的格式,您可以使用此方法以多種格式儲存資料。 使用此方法儲存的資料可以在擷取時轉換成相容的格式。

SetData(Object) 載會 dataObject.GetType 呼叫 方法所決定的格式儲存值。 如果 data 實作 ISerializable 介面,此多載也會以 Serializable 格式儲存值。

另請參閱

適用於

SetData(String, Object)

使用指定的格式,將指定的物件新增至 DataObject

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)

參數

format
String

與資料建立關聯的格式。 如需預先定義的格式,請參閱 DataFormats

data
Object

要儲存的資料。

實作

範例

下列程式碼範例會將資料儲存在 中 DataObject ,並將其格式指定為 Unicode。

然後,藉由指定文字格式來擷取資料,因為預設值是在最終格式相容時轉換資料。 結果會顯示在文字方塊中。

此程式碼需要 textBox1 已建立。

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

備註

重要

使用不信任的資料呼叫此方法,會造成安全性上的風險。 呼叫此方法時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入

如果您不知道目標應用程式的格式,您可以使用此方法以多種格式儲存資料。

使用此方法儲存的資料可以在擷取時轉換成相容的格式。

另請參閱

適用於

SetData(Type, Object)

使用指定的類型作為格式,將指定的物件新增至 DataObject

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)

參數

format
Type

Type 表示與資料相關的格式。

data
Object

要儲存的資料。

實作

範例

下列程式碼範例會使用 作為資料格式,將資料 Type 儲存在 DataObject 中。 然後,呼叫 Type 以指定資料格式來擷 GetData 取資料。 結果會顯示在文字方塊中。

此程式碼需要 textBox1 已建立。

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

備註

重要

使用不信任的資料呼叫此方法,會造成安全性上的風險。 呼叫此方法時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入

如果您不知道目標應用程式的格式,您可以使用此方法以多種格式儲存資料。

使用此方法儲存的資料可以在擷取時轉換成相容的格式。

另請參閱

適用於

SetData(String, Boolean, Object)

使用指定的格式,將指定的物件新增至 DataObject,並指出該資料是否可以轉換成另一種格式。

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)

參數

format
String

與資料建立關聯的格式。 如需預先定義的格式,請參閱 DataFormats

autoConvert
Boolean

若資料可以被轉換成另一種格式,則為 true;否則為 false

data
Object

要儲存的資料。

實作

範例

下列程式碼範例會將資料儲存在 中 DataObject ,並指定資料只能以其原生格式擷取。

首先,會建立新的 DataObject 。 Unicode 格式的資料會儲存在 中 DataObject ,並將 autoConvert 設定為 false

然後, DataObject 會查詢 以取得可用資料格式的清單。 雖然 Unicode 資料可以轉換成文字和其他格式,但只會傳回 Unicode 格式。

此程式碼需要 textBox1 已建立。

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

備註

重要

使用不信任的資料呼叫此方法,會造成安全性上的風險。 呼叫此方法時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入

如果您不知道目標應用程式的格式,您可以使用此方法以多種格式儲存資料。

另請參閱

適用於