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. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

返回

与指定格式关联的数据,或为 nullThe 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的数据, 该数据Text与格式相关联。The method is used to retrieve the data stored in myDataObject, which is associated with the Text format. 该示例假设已创建Form一个名Form1为的和一个TextBox textBox1名为的。The 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 GetData前调用。To 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. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

返回

与指定格式关联的数据,或为 nullThe 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的数据, 该数据与特定myType类型相关联。The 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名为Form1的。The 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 GetData前调用。To 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. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

autoConvert
Boolean

将数据转换成指定格式,值为 true;反之,值为 falsetrue to convert the data to the specified format; otherwise, false.

返回

与指定格式关联的数据,或为 nullThe 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并将false参数设置为。In the first trial, it specifies its format as a string and sets the autoConvert parameter to false. 此试用版失败, 结果显示在标有 "Message #1" 的消息框中。This trial fails, and the result is displayed in a message box labeled "Message #1." 在第二次试验中, 该示例将检索autoConvert参数设置为true的相同数据。In the second trial, the example retrieves the same data with the autoConvert parameter set to true. 此试用版成功, 结果显示在标有 "Message #2" 的消息框中。This trial succeeds, and the result is displayed in a message box labeled "Message #2." 该示例假设您已创建了一个FormForm1为的。The 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

注解

如果参数为true , 并且此方法无法找到具有指定格式的数据, 则它会尝试将数据转换为格式。 autoConvertIf 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的情况下存储的, 则此方法null返回。If 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.

如果参数为false, 则此方法返回指定格式null的数据; 如果找不到此格式的数据, 则为。 autoConvertIf 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 GetData前调用。To 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.

另请参阅

适用于