DataObject.GetData DataObject.GetData DataObject.GetData DataObject.GetData Method

定义

返回与所指定数据格式关联的数据。Returns the data associated with the specified data format.

重载

GetData(String, Boolean) GetData(String, Boolean) GetData(String, Boolean) GetData(String, Boolean)

返回与所指定数据格式关联的数据,使用自动转换参数来确定是否将数据转换为此格式。Returns the data associated with the specified data format, using an automated conversion parameter to determine whether to convert the data to the format.

GetData(String) GetData(String) GetData(String) GetData(String)

返回与所指定数据格式关联的数据。Returns the data associated with the specified data format.

GetData(Type) GetData(Type) GetData(Type) GetData(Type)

返回与所指定类类型格式关联的数据。Returns the data associated with the specified class type format.

GetData(String, Boolean) GetData(String, Boolean) GetData(String, Boolean) GetData(String, Boolean)

返回与所指定数据格式关联的数据,使用自动转换参数来确定是否将数据转换为此格式。Returns the data associated with the specified data format, using an automated conversion parameter to determine whether to convert the data to the format.

public:
 virtual System::Object ^ GetData(System::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 String String String

要检索的数据的格式。The format of the data to retrieve. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

autoConvert
Boolean Boolean Boolean Boolean

如果要将数据转换成指定的格式,则为 true;否则为 falsetrue to the convert data to the specified format; otherwise, false.

返回

与指定格式关联的数据,或为 nullThe data associated with the specified format, or null.

实现

示例

下面的代码示例DataObject autoConvert使用参数指定是否转换数据格式, 从而检索中存储的数据。The following code example retrieves the data stored in a DataObject, using the autoConvert parameter to specify whether to convert the data format.

首先, 使用文本DataObject数据创建新的。First, a new DataObject is created with text data. 然后, 该示例尝试检索数据, 并将其格式指定为字符串而不进行格式转换, 也就是说, autoConvert参数是。 falseThen the example tries to retrieve the data, specifying its format as a string and no format conversion, that is, the autoConvert parameter is false. 此操作失败, 因为中DataObject没有字符串数据。This operation fails because there is no string data in the DataObject.

接下来, 该示例尝试再次检索数据, 并autoConvert将参数设置为。 trueNext, the example tries to retrieve the data again, with the autoConvert parameter set to true. 此操作成功, 结果显示在中MessageBoxThis operation succeeds and the results are displayed in a MessageBox.

此代码需要已textBox1创建的。This code requires that textBox1 has been created.

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 'GetMyData3

注解

如果参数为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. 如果数据无法转换为指定的格式, 或者如果数据是在自动转换设置为false的情况下存储的, 则此方法将返回。 nullIf the data cannot be converted to the specified format, or if the data was stored with the automatic conversion 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以获取此DataObject中存储的数据的有效格式列表。Call GetFormats for a list of valid formats for the data stored in this DataObject.

备注

如果数据已存储, 则可以将其转换为另一种格式, 这种格式指定允许该转换, 并且请求的格式与存储格式兼容。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.

formatHtml时, 此方法将在以 .net 4.5 或更高版本为目标的应用程序中返回 utf-8 编码的字符串, 并在面向 .net 4.0 或更低版本的应用程序中返回 ANSI 编码的字符串。When format is Html, this method returns a UTF-8 encoded string in applications that target .NET 4.5 or higher, and an ANSI encoded string in applications that target .NET 4.0 or lower.

另请参阅

GetData(String) GetData(String) GetData(String) GetData(String)

返回与所指定数据格式关联的数据。Returns the data associated with the specified data format.

public:
 virtual System::Object ^ GetData(System::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 String String String

要检索的数据的格式。The format of the data to retrieve. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

返回

与指定格式关联的数据,或为 nullThe data associated with the specified format, or null.

实现

示例

下面的代码示例检索中DataObject存储的数据。The following code example retrieves the data stored in a DataObject. 首先, 使用文本DataObject数据创建新的。First, a new DataObject is created with text data. 然后, 检索数据, 将其格式指定为字符串, 并在文本框中显示。Then, the data is retrieved, specifying its format as a string, and displayed in a text box.

此代码需要已textBox1创建的。This code requires that textBox1 has been created.

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 'AddMyData3

注解

如果此方法无法找到具有指定格式的数据, 则会尝试将数据转换为格式。If this method cannot find data in the specified format, it attempts to convert the data to the format. 如果数据无法转换为指定的格式, 或者, 如果数据是通过自动转换设置为false存储的, 则此方法将返回。 nullIf the data cannot be converted to the specified format, or if the data was stored with automatic conversion set to false, 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以获取此DataObject中存储的数据的有效格式列表。Call GetFormats for a list of valid formats for the data stored in this DataObject.

备注

如果数据已存储, 则可以将其转换为另一种格式, 这种格式指定允许该转换, 并且请求的格式与存储格式兼容。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.

formatHtml时, 此方法将在以 .net 4.5 或更高版本为目标的应用程序中返回 utf-8 编码的字符串, 并在面向 .net 4.0 或更低版本的应用程序中返回 ANSI 编码的字符串。When format is Html, this method returns a UTF-8 encoded string in applications that target .NET 4.5 or higher, and an ANSI encoded string in applications that target .NET 4.0 or lower.

另请参阅

GetData(Type) GetData(Type) GetData(Type) GetData(Type)

返回与所指定类类型格式关联的数据。Returns the data associated with the specified class type format.

public:
 virtual System::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 Type Type

Type 表示要检索的数据的格式。A Type representing the format of the data to retrieve.

返回

与指定格式关联的数据,或为 nullThe data associated with the specified format, or null.

实现

示例

下面的代码示例检索中DataObject存储的数据。The following code example retrieves the data stored in a DataObject. 首先, 使用组件DataObject创建新的。First, a new DataObject is created with a component. 然后, 将检索数据并指定其类型。Then, the data is retrieved, specifying its type. 检索到的数据的类型会显示在文本框中。The type of the retrieved data is displayed in a text box.

此代码需要已textBox1创建的。This code requires that textBox1 has been created.

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 'GetMyData

注解

如果此方法无法找到具有指定格式的数据, 则会尝试将数据转换为格式。If this method cannot find data in the specified format, it attempts to convert the data to the format. 如果数据无法转换为指定的格式, 或者, 如果数据是通过自动转换设置为false存储的, 则此方法将返回。 nullIf the data cannot be converted to the specified format, or if the data was stored with automatic conversion set to false, 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以获取此DataObject中存储的数据的有效格式列表。Call GetFormats for a list of valid formats for the data stored in this DataObject.

备注

如果数据已存储, 则可以将其转换为另一种格式, 这种格式指定允许该转换, 并且请求的格式与存储格式兼容。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.

另请参阅

适用于