IDataObject.GetData 方法

定義

擷取與指定資料格式產生關聯的資料。Retrieves the data associated with the specified data format.

多載

GetData(String)

擷取與指定資料格式產生關聯的資料。Retrieves the data associated with the specified data format.

GetData(Type)

擷取與指定的類別型別格式相關的資料。Retrieves the data associated with the specified class type format.

GetData(String, Boolean)

擷取與指定的資料格式相關的資料,使用布林值來決定是否要將資料轉換成格式。Retrieves the data associated with the specified data format, using a Boolean to determine whether to convert the data to the format.

GetData(String)

擷取與指定資料格式產生關聯的資料。Retrieves the data associated with the specified data format.

public:
 System::Object ^ GetData(System::String ^ format);
public object GetData (string format);
abstract member GetData : string -> obj
Public Function GetData (format As String) As Object

參數

format
String

要擷取的資料格式。The format of the data to retrieve. 如需預先定義的格式,請參閱 DataFormatsSee DataFormats for predefined formats.

傳回

Object

與所指定格式或 null 建立關聯的資料。The data associated with the specified format, or null.

範例

這個範例會使用 DataObject 類別,此類別會 IDataObject, 示範如何使用 GetData 方法。This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetData method. 方法是用來取出儲存在中的資料 myDataObject ,該資料與格式相關聯 TextThe method is used to retrieve the data stored in myDataObject, which is associated with the Text format. 此範例假設您已建立名為的 Form Form1 和名為 TextBoxtextBox1The example assumes that you have already created a Form named Form1 and a TextBox named textBox1.

private:
   void GetData1()
   {
      // Creates a new data object using a string and the text format.
      String^ myString = "My text string";
      DataObject^ myDataObject = gcnew DataObject( DataFormats::Text,myString );

      // Displays the string in a text box.
      textBox1->Text = myDataObject->GetData( DataFormats::Text )->ToString();
   }
private void GetData1() 
{
    // Creates a new data object using a string and the text format.
    string myString = "My text string";
    DataObject myDataObject = new DataObject(DataFormats.Text, myString);

    // Displays the string in a text box.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).ToString();
}
Private Sub GetData1()
    ' Creates a new data object using a string and the text format.
    Dim myString As String = "My text string"
    Dim myDataObject As New DataObject(DataFormats.Text, myString)

    ' Displays the string in a text box.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).ToString()
End Sub

備註

如果這個方法找不到指定格式的資料,它會嘗試將資料轉換成格式。If this method cannot find data in the specified format, it attempts to convert the data to the format. 如果無法將資料轉換成指定的格式,這個方法會傳回 nullIf the data cannot be converted to the specified format, this method returns null.

若要判斷資料是否與相關聯,或是可以轉換成格式,請 GetDataPresent 在呼叫之前呼叫 GetDataTo determine whether data is associated with, or can be converted to, a format, call GetDataPresent before calling GetData. GetFormats針對儲存在此實例中的資料,呼叫有效格式的清單。Call GetFormats for a list of valid formats for the data stored in this instance.

注意

如果資料儲存指定允許轉換,而且要求的格式與預存格式相容,則可以將資料轉換成另一種格式。Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. 例如,儲存為 Unicode 的資料可以轉換成文字。For example, data stored as Unicode can be converted to text.

如需這個方法的執行方式,請參閱 DataObject.GetDataFor an implementation of this method, see DataObject.GetData.

另請參閱

適用於

GetData(Type)

擷取與指定的類別型別格式相關的資料。Retrieves the data associated with the specified class type format.

public:
 System::Object ^ GetData(Type ^ format);
public object GetData (Type format);
abstract member GetData : Type -> obj
Public Function GetData (format As Type) As Object

參數

format
Type

Type,表示要擷取的資料的格式。A Type representing the format of the data to retrieve. 如需預先定義的格式,請參閱 DataFormatsSee DataFormats for predefined formats.

傳回

Object

與所指定格式或 null 建立關聯的資料。The data associated with the specified format, or null.

範例

這個範例會使用實作為的 DataObject 類別 IDataObject 來示範如何使用 GetData 方法。This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetData method. 方法是用來取出儲存在中的資料 myObject (與特定類型相關聯) myTypeThe method is used to retrieve the data stored in myObject, which is associated with a specific type, myType. 抓取的資料類型會顯示在訊息方塊中。The type of the retrieved data is displayed in a message box. 此範例假設您已建立 Form 名為的 Form1The example assumes that you have already created a Form named Form1.

private:
   void GetData2()
   {
      // Creates a component.
      Component^ myComponent = gcnew Component;

      // Creates a data object, and assigns it the component.
      DataObject^ myDataObject = gcnew DataObject( myComponent );

      // Creates a type, myType, 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 )
            MessageBox::Show( "The data type stored in the data object is " +
                  myObject->GetType()->Name + "." );
      else
            MessageBox::Show( "Data of the specified type was not stored in the data object." );
   }
       private void GetData2() 
       {
           // Creates a component.
           Component myComponent = new Component();

           // Creates a data object, and assigns it the component.
           DataObject myDataObject = new DataObject(myComponent);

           // Creates a type, myType, 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)
               MessageBox.Show("The data type stored in the data object is " +
                   myObject.GetType().Name + ".");
           else
               MessageBox.Show("Data of the specified type was not stored " +
                   "in the data object.");
       }
Private Sub GetData2()
    ' Creates a component.
    Dim myComponent As New System.ComponentModel.Component()

    ' Creates a data object, and assigns it the component.
    Dim myDataObject As New DataObject(myComponent)

    ' Creates a type, myType, 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
        MessageBox.Show("The data type stored in the data object is " + myObject.GetType().Name + ".")
    Else
        MessageBox.Show("Data of the specified type was not stored " + "in the data object.")
    End If
End Sub

備註

如果這個方法找不到指定格式的資料,它會嘗試將資料轉換成格式。If this method cannot find data in the specified format, it attempts to convert the data to the format. 如果無法將資料轉換成指定的格式,這個方法會傳回 nullIf the data cannot be converted to the specified format, this method returns null.

若要判斷資料是否與相關聯,或是可以轉換成格式,請 GetDataPresent 在呼叫之前呼叫 GetDataTo determine whether data is associated with, or can be converted to, a format, call GetDataPresent before calling GetData. GetFormats針對儲存在此實例中的資料,呼叫有效格式的清單。Call GetFormats for a list of valid formats for the data stored in this instance.

注意

如果資料儲存指定允許轉換,而且要求的格式與預存格式相容,則可以將資料轉換成另一種格式。Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. 例如,儲存為 Unicode 的資料可以轉換成文字。For example, data stored as Unicode can be converted to text.

如需這個方法的執行方式,請參閱 DataObject.GetDataFor an implementation of this method, see DataObject.GetData.

另請參閱

適用於

GetData(String, Boolean)

擷取與指定的資料格式相關的資料,使用布林值來決定是否要將資料轉換成格式。Retrieves the data associated with the specified data format, using a Boolean to determine whether to convert the data to the format.

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

參數

format
String

要擷取的資料格式。The format of the data to retrieve. 如需預先定義的格式,請參閱 DataFormatsSee DataFormats for predefined formats.

autoConvert
Boolean

若要將資料轉換成指定的格式則為 true,否則為 falsetrue to convert the data to the specified format; otherwise, false.

傳回

Object

與所指定格式或 null 建立關聯的資料。The data associated with the specified format, or null.

範例

這個範例會使用實作為的 DataObject 類別 IDataObject 來示範如何使用 GetData 方法。This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetData method. 此範例會 DataObject 使用 autoConvert 參數來指定是否要轉換資料格式,以抓取儲存在中的資料。The example retrieves the data stored in a DataObject, using the autoConvert parameter to specify whether or not to convert the data format. 首先, myDataObject 使用文字資料建立。First, myDataObject is created with text data. 然後,此範例會嘗試兩次取出資料。Then the example tries twice to retrieve the data. 在第一次試用中,它會將其格式指定為字串,並將 autoConvert 參數設定為 falseIn the first trial, it specifies its format as a string and sets the autoConvert parameter to false. 此試用失敗,結果會顯示在標示為「訊息 #1」的訊息方塊中。This trial fails, and the result is displayed in a message box labeled "Message #1." 在第二個試用版中,此範例會抓取 autoConvert 參數設定為的相同資料 trueIn the second trial, the example retrieves the same data with the autoConvert parameter set to true. 此試用版會成功,而且結果會顯示在標示為「訊息 #2」的訊息方塊中。This trial succeeds, and the result is displayed in a message box labeled "Message #2." 此範例假設您已建立 Form 名為的 Form1The example assumes that you have created a Form named Form1.

private:
   void GetData3()
   {
      // Creates a new data object using a text string.
      String^ myString = "Hello World!";
      DataObject^ myDataObject = gcnew DataObject( DataFormats::Text,myString );

      // Displays the string with autoConvert equal to false.
      if ( myDataObject->GetData( "System::String", false ) != nullptr )
      {
         // Displays the string in a message box.
         MessageBox::Show( myDataObject->GetData( "System::String", false ) + ".", "Message #1" );
      }
      else
            MessageBox::Show( "Could not find data of the specified format.", "Message #1" );

      // Displays a not found message in a message box.
      // Displays the string in a text box with autoConvert equal to true.
      String^ myData = "The data is " + myDataObject->GetData( "System::String", true ) + ".";
      MessageBox::Show( myData, "Message #2" );
   }
       private void GetData3() 
       {
           // Creates a new data object using a text string.
           string myString = "Hello World!";
           DataObject myDataObject = new DataObject(DataFormats.Text, myString);

           // Displays the string with autoConvert equal to false.
           if (myDataObject.GetData("System.String", false) != null) 
           {
               // Displays the string in a message box.
               MessageBox.Show(myDataObject.GetData("System.String", false).ToString() + ".", "Message #1");
           } 
           else
           {
               // Displays a not found message in a message box.
               MessageBox.Show("Could not find data of the specified format.", "Message #1");
           }

           // Displays the string in a text box with autoConvert equal to true.
           string myData = "The data is " + myDataObject.GetData("System.String", true).ToString() +".";
           MessageBox.Show(myData,"Message #2");
       }
Private Sub GetData3()
    ' Creates a new data object using a text string.
    Dim myString As String = "Hello World!"
    Dim myDataObject As New DataObject(DataFormats.Text, myString)

    ' Displays the string with autoConvert equal to false.
    If (myDataObject.GetData("System.String", False) IsNot Nothing) Then
        ' Displays the string in a message box.
        MessageBox.Show(myDataObject.GetData("System.String", False).ToString() + ".", "Message #1")
        ' Displays a not found message in a message box.
    Else
        MessageBox.Show("Could not find data of the specified format.", "Message #1")
    End If

    ' Displays the string in a text box with autoConvert equal to true.
    Dim myData As String = "The data is " + myDataObject.GetData("System.String", True).ToString()
    MessageBox.Show(myData, "Message #2")
End Sub

備註

如果 autoConvert 參數為, true 而且這個方法找不到指定格式的資料,它會嘗試將資料轉換成格式。If the autoConvert parameter is true and this method cannot find data in the specified format, it attempts to convert the data to the format. 如果無法將資料轉換成指定的格式,或如果資料是以 autoConvert 設定為的參數儲存 false ,這個方法會傳回 nullIf the data cannot be converted to the specified format, or if the data was stored with the autoConvert parameter set to false, this method returns null.

如果 autoConvert 參數為 false ,這個方法會以指定的格式傳回資料, null 如果找不到此格式的資料,則為。If the autoConvert parameter is false, this method returns data in the specified format, or null if no data in this format can be found.

若要判斷資料是否與相關聯,或是可以轉換成格式,請 GetDataPresent 在呼叫之前呼叫 GetDataTo determine whether data is associated with, or can be converted to, a format, call GetDataPresent before calling GetData. GetFormats針對儲存在此實例中的資料,呼叫有效格式的清單。Call GetFormats for a list of valid formats for the data stored in this instance.

注意

如果資料儲存指定允許轉換,而且要求的格式與預存格式相容,則可以將資料轉換成另一種格式。Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. 例如,儲存為 Unicode 的資料可以轉換成文字。For example, data stored as Unicode can be converted to text.

如需這個方法的執行方式,請參閱 DataObject.GetDataFor an implementation of this method, see DataObject.GetData.

另請參閱

適用於