DataObject.SetData 方法

定義

在 中加入一個物件。DataObject

多載

名稱 Description
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);
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

備註

Important

使用不受信任的數據呼叫此方法是安全性風險。 僅使用信任的數據呼叫這個方法。 如需詳細資訊,請參閱 驗證所有輸入

如果你不知道目標應用程式的格式,也可以用這種方法以多種格式儲存資料。 使用此方法儲存的資料在檢索時可轉換為相容格式。

過載會SetData(Object)以呼叫方法決定Object.GetType的格式儲存該data值。 如果 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);
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

備註

Important

使用不受信任的數據呼叫此方法是安全性風險。 僅使用信任的數據呼叫這個方法。 如需詳細資訊,請參閱 驗證所有輸入

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

使用此方法儲存的資料在檢索時可轉換為相容格式。

另請參閱

適用於

SetData(Type, Object)

使用指定的類型作為格式,將指定的物件加入 。DataObject

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)

參數

format
Type

A Type 代表與資料相關的格式。

data
Object

要儲存的資料。

實作

範例

以下程式碼範例以 a Type 作為資料格式儲存資料DataObject。 然後透過呼叫 GetDataType 指定資料格式來取得資料。 結果會顯示在文字框中。

這套規範要求已經建立。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

備註

Important

使用不受信任的數據呼叫此方法是安全性風險。 僅使用信任的數據呼叫這個方法。 如需詳細資訊,請參閱 驗證所有輸入

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

使用此方法儲存的資料在檢索時可轉換為相容格式。

另請參閱

適用於

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);
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

要儲存的資料。

實作

範例

以下程式碼範例將資料儲存在 a DataObject ,並指定資料只能以原生格式檢索。

首先,創造一個新的 DataObject 。 Unicode 格式的資料儲存在 DataObject,並設 autoConvertfalse

接著, 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

備註

Important

使用不受信任的數據呼叫此方法是安全性風險。 僅使用信任的數據呼叫這個方法。 如需詳細資訊,請參閱 驗證所有輸入

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

另請參閱

適用於