DataObject.GetData 方法

定義

傳回與所指定資料格式相關的資料。

多載

GetData(String, Boolean)

傳回與所指定資料格式相關的資料,使用自動轉換參數來決定是否要將資料轉換成格式。

GetData(String)

傳回與所指定資料格式相關的資料。

GetData(Type)

傳回與所指定類別類型格式相關的資料。

GetData(String, Boolean)

傳回與所指定資料格式相關的資料,使用自動轉換參數來決定是否要將資料轉換成格式。

public:
 virtual System::Object ^ GetData(System::String ^ format, bool autoConvert);
public virtual object GetData (string format, bool autoConvert);
public virtual object? GetData (string format, bool autoConvert);
abstract member GetData : string * bool -> obj
override this.GetData : string * bool -> obj
Public Overridable Function GetData (format As String, autoConvert As Boolean) As Object

參數

format
String

要擷取的資料格式。 如需預先定義的格式,請參閱 DataFormats

autoConvert
Boolean

若要將資料轉換成指定的格式,則為 true;否則為 false

傳回

與所指定格式或 null 建立關聯的資料。

實作

範例

下列程式碼範例會使用 autoConvert 參數來指定是否要轉換資料格式,擷取儲存在 中的資料 DataObject

首先,會使用文字資料建立新的 DataObject 。 然後,此範例會嘗試擷取資料,並將其格式指定為字串,而且沒有格式轉換,也就是 autoConvert 參數為 false 。 這項作業失敗,因為 中 DataObject 沒有字串資料。

接下來,此範例會嘗試再次擷取資料,並將 autoConvert 參數設定為 true 。 此作業成功,結果會顯示在 中 MessageBox

此程式碼需要 textBox1 已建立。

private:
   void GetMyData3()
   {
      // Creates a new data object using a string and the text format.
      String^ myString = "My new text string";
      DataObject^ myDataObject = gcnew DataObject( DataFormats::Text,myString );
      
      // Prints the string in a text box with autoconvert = false.
      if ( myDataObject->GetData( "System.String", false ) != 0 )
      {
         // Prints the string in a text box.
         textBox1->Text = String::Concat(
            myDataObject->GetData( "System.String", false )->ToString(), "\n" );
      }
      else
      {
         textBox1->Text = "Could not find data of the specified format\n";
      }
      
      // Prints the string in a text box with autoconvert = true.
      textBox1->Text = String::Concat(
            textBox1->Text, myDataObject->GetData( "System.String", true )->ToString() );
   }
private void GetMyData3() {
    // Creates a new data object using a string and the text format.
    string myString = "My new text string";
    DataObject myDataObject = new DataObject(DataFormats.Text, myString);
 
    // Prints the string in a text box with autoconvert = false.
    if(myDataObject.GetData("System.String", false) != null) {
       // Prints the string in a text box.
       textBox1.Text = myDataObject.GetData("System.String", false).ToString() + '\n';
    } else
        {
            textBox1.Text = "Could not find data of the specified format" + '\n';
        }

        // Prints the string in a text box with autoconvert = true.
        textBox1.Text += myDataObject.GetData("System.String", true).ToString();
 }
Private Sub GetMyData3()
    ' Creates a new data object using a string and the text format.
    Dim myString As String = "My new text string"
    Dim myDataObject As New DataObject(DataFormats.Text, myString)
    
    ' Prints the string in a text box with autoconvert = false.
    If (myDataObject.GetData("System.String", False) IsNot Nothing) Then
        ' Prints the string in a text box.
        textBox1.Text = myDataObject.GetData("System.String", False).ToString() & ControlChars.Cr
    Else
        textBox1.Text = "Could not find data of the specified format" & ControlChars.Cr
    End If 
    ' Prints the string in a text box with autoconvert = true.
    textBox1.Text += myDataObject.GetData("System.String", True).ToString()
End Sub

備註

autoConvert如果 參數為 true ,而且這個方法找不到指定格式的資料,它會嘗試將資料轉換成格式。 如果資料無法轉換成指定的格式,或資料是以自動轉換設定 false 為 儲存,則這個方法會傳 null 回 。

autoConvert如果 參數為 false ,則這個方法會傳回指定格式的資料,如果 null 找不到此格式的資料,則為 。

若要判斷資料是否與 相關聯,或可以轉換成 格式,請在呼叫 之前呼叫 GetDataPresentGetData 。 針對儲存在此 DataObject 中的資料,呼叫 GetFormats 有效格式的清單。

注意

如果資料儲存指定允許轉換,而且要求的格式與預存格式相容,則可以將資料轉換成另一種格式。 例如,儲存為 Unicode 的資料可以轉換成文字。

當 為 Htmlformat ,這個方法會在以 .NET 4.5 或更高版本為目標的應用程式中傳回 UTF-8 編碼字串,以及以 .NET 4.0 或更低為目標之應用程式中的 ANSI 編碼字串。

另請參閱

適用於

GetData(String)

傳回與所指定資料格式相關的資料。

public:
 virtual System::Object ^ GetData(System::String ^ format);
public virtual object GetData (string format);
public virtual object? GetData (string format);
abstract member GetData : string -> obj
override this.GetData : string -> obj
Public Overridable Function GetData (format As String) As Object

參數

format
String

要擷取的資料格式。 如需預先定義的格式,請參閱 DataFormats

傳回

與所指定格式或 null 建立關聯的資料。

實作

範例

下列程式碼範例會擷取儲存在 中的資料 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,
            " is present in the DataObject" );
      }
      else
      {
         textBox1->Text = String::Concat( "Data of type ", myType,
            " 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.ToString() + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.ToString() +
       " 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.ToString() & _
            " is present in the DataObject"
    Else
        textBox1.Text = "Data of type " & myType.ToString() & _
            " is not present in the DataObject"
    End If
End Sub

備註

如果這個方法找不到指定格式的資料,它會嘗試將資料轉換成格式。 如果資料無法轉換成指定的格式,或資料是以自動轉換設定 false 為 儲存,則這個方法會傳 null 回 。

若要判斷資料是否與 相關聯,或可以轉換成 格式,請在呼叫 之前呼叫 GetDataPresentGetData 。 針對儲存在此 DataObject 中的資料,呼叫 GetFormats 有效格式的清單。

注意

如果資料儲存指定允許轉換,而且要求的格式與預存格式相容,則可以將資料轉換成另一種格式。 例如,儲存為 Unicode 的資料可以轉換成文字。

當 為 Htmlformat ,這個方法會在以 .NET 4.5 或更高版本為目標的應用程式中傳回 UTF-8 編碼字串,以及以 .NET 4.0 或更低為目標之應用程式中的 ANSI 編碼字串。

另請參閱

適用於

GetData(Type)

傳回與所指定類別類型格式相關的資料。

public:
 virtual System::Object ^ GetData(Type ^ format);
public virtual object GetData (Type format);
public virtual object? GetData (Type format);
abstract member GetData : Type -> obj
override this.GetData : Type -> obj
Public Overridable Function GetData (format As Type) As Object

參數

format
Type

Type,表示要擷取的資料的格式。

傳回

與所指定格式或 null 建立關聯的資料。

實作

範例

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

此程式碼需要 textBox1 已建立。

private:
   void GetMyData()
   {
      // Creates a component to store in the data object.
      Component^ myComponent = gcnew Component;
      
      // Creates a new data object and assigns it the component.
      DataObject^ myDataObject = gcnew DataObject( myComponent );
      
      // Creates a type to store the type of data.
      Type^ myType = myComponent->GetType();
      
      // Retrieves the data using myType to represent its type.
      Object^ myObject = myDataObject->GetData( myType );
      if ( myObject != nullptr )
      {
         textBox1->Text = String::Format( "The data type stored in the DataObject is: {0}",
            myObject->GetType()->Name );
      }
      else
      {
         textBox1->Text = "Data of the specified type was not stored in the DataObject.";
      }
   }
private void GetMyData() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Creates a new data object and assigns it the component.
    DataObject myDataObject = new DataObject(myComponent);
 
    // Creates a type to store the type of data.
    Type myType = myComponent.GetType();
 
    // Retrieves the data using myType to represent its type.
    Object myObject = myDataObject.GetData(myType);
    if(myObject != null)
       textBox1.Text = "The data type stored in the DataObject is: " +
       myObject.GetType().Name;
    else
       textBox1.Text = "Data of the specified type was not stored " +
       "in the DataObject.";
 }
Private Sub GetMyData()
    ' Creates a component to store in the data object.
    Dim myComponent As New Component()
    
    ' Creates a new data object and assigns it the component.
    Dim myDataObject As New DataObject(myComponent)
    
    ' Creates a type to store the type of data.
    Dim myType As Type = myComponent.GetType()
    
    ' Retrieves the data using myType to represent its type.
    Dim myObject As Object = myDataObject.GetData(myType)
    If (myObject IsNot Nothing) Then
        textBox1.Text = "The data type stored in the DataObject is: " & myObject.GetType().Name
    Else
        textBox1.Text = "Data of the specified type was not stored " & "in the DataObject."
    End If
End Sub

備註

如果這個方法找不到指定格式的資料,它會嘗試將資料轉換成格式。 如果資料無法轉換成指定的格式,或資料是以自動轉換設定 false 為 儲存,則這個方法會傳 null 回 。

若要判斷資料是否與 相關聯,或可以轉換成 格式,請在呼叫 之前呼叫 GetDataPresentGetData 。 針對儲存在此 DataObject 中的資料,呼叫 GetFormats 有效格式的清單。

注意

如果資料儲存指定允許轉換,而且要求的格式與預存格式相容,則可以將資料轉換成另一種格式。 例如,儲存為 Unicode 的資料可以轉換成文字。

另請參閱

適用於