DataObject.GetDataPresent 方法

定义

确定数据是可用的,还是可以转换为指定的格式。

重载

名称 说明
GetDataPresent(String)

确定数据是否可用,或者是否可以转换为字符串指定的格式。

GetDataPresent(Type)

确定数据是可用的,还是可以转换为对象指定的 Type 格式。

GetDataPresent(String, Boolean)

确定数据是可用的,还是可以转换为指定的格式。 标志 Boolean 指示是否可检查数据是否可以转换为指定格式(如果该格式不可用)。

注解

调用 GetDataPresent 以确定格式是否在此数据对象中可用,然后再调用 GetData。 调用 GetFormats 以获取此数据对象中提供的所有格式的列表。

GetDataPresent(String)

确定数据是否可用,或者是否可以转换为字符串指定的格式。

public:
 virtual bool GetDataPresent(System::String ^ format);
public bool GetDataPresent(string format);
abstract member GetDataPresent : string -> bool
override this.GetDataPresent : string -> bool
Public Function GetDataPresent (format As String) As Boolean

参数

format
String

一个字符串,指定数据的格式。 有关一组预定义的数据格式,请参阅该 DataFormats 类。

返回

true 如果数据处于指定格式或可转换为,则为否则,为 false.

实现

例外

formatnull

示例

下面的示例使用此方法通过描述符字符串查询是否存在特定数据格式。

DataObject dataObject = new DataObject("Some string data to store...");

// Query for the presence of Text data in the data object, by a data format descriptor string.
// In this overload of GetDataPresent, the method will return true both for native data formats
// and when the data can automatically be converted to the specifed format.

// In this case, string data is present natively, so GetDataPresent returns "true".
string textData = null;
if (dataObject.GetDataPresent(DataFormats.StringFormat))
{
    textData = dataObject.GetData(DataFormats.StringFormat) as string;
}

// In this case, the Text data in the data object can be autoconverted to 
// Unicode text, so GetDataPresent returns "true".
byte[] unicodeData = null;
if (dataObject.GetDataPresent(DataFormats.UnicodeText))
{
    unicodeData = dataObject.GetData(DataFormats.UnicodeText) as byte[];
}
Dim dataObject As New DataObject("Some string data to store...")

' Query for the presence of Text data in the data object, by a data format descriptor string.
' In this overload of GetDataPresent, the method will return true both for native data formats
' and when the data can automatically be converted to the specifed format.

' In this case, string data is present natively, so GetDataPresent returns "true".
Dim textData As String = Nothing
If dataObject.GetDataPresent(DataFormats.StringFormat) Then
    textData = TryCast(dataObject.GetData(DataFormats.StringFormat), String)
End If

' In this case, the Text data in the data object can be autoconverted to 
' Unicode text, so GetDataPresent returns "true".
Dim unicodeData() As Byte = Nothing
If dataObject.GetDataPresent(DataFormats.UnicodeText) Then
    unicodeData = TryCast(dataObject.GetData(DataFormats.UnicodeText), Byte())
End If

注解

调用 GetDataPresent 以确定格式是否在此数据对象中可用,然后再调用 GetData。 调用 GetFormats 以获取此数据对象中提供的所有格式的列表。

另请参阅

适用于

GetDataPresent(Type)

确定数据是可用的,还是可以转换为对象指定的 Type 格式。

public:
 virtual bool GetDataPresent(Type ^ format);
public bool GetDataPresent(Type format);
abstract member GetDataPresent : Type -> bool
override this.GetDataPresent : Type -> bool
Public Function GetDataPresent (format As Type) As Boolean

参数

format
Type

指定要检查的数据格式的 A Type 。 F 或一组预定义的数据格式,请参阅类 DataFormats

返回

true 如果数据处于指定格式或可转换为,则为否则,为 false.

实现

例外

formatnull

示例

下面的示例使用此方法按类型查询是否存在特定数据格式。

DataObject dataObject = new DataObject("Some string data to store...");

// Query for the presence of String data in the data object, by type.  In this overload 
// of GetDataPresent, the method will return true both for native data formats
// and when the data can automatically be converted to the specifed format.

// In this case, the Text data present in the data object can be autoconverted
// to type string (also represented by DataFormats.String), so GetDataPresent returns "true".
string stringData = null;
if (dataObject.GetDataPresent(typeof(string)))
{
    stringData = dataObject.GetData(DataFormats.Text) as string;
}
Dim dataObject As New DataObject("Some string data to store...")

' Query for the presence of String data in the data object, by type.  In this overload 
' of GetDataPresent, the method will return true both for native data formats
' and when the data can automatically be converted to the specifed format.

' In this case, the Text data present in the data object can be autoconverted
' to type string (also represented by DataFormats.String), so GetDataPresent returns "true".
Dim stringData As String = Nothing
If dataObject.GetDataPresent(GetType(String)) Then
    stringData = TryCast(dataObject.GetData(DataFormats.Text), String)
End If

注解

调用 GetDataPresent 以确定格式是否在此数据对象中可用,然后再调用 GetData。 调用 GetFormats 以获取此数据对象中提供的所有格式的列表。

另请参阅

适用于

GetDataPresent(String, Boolean)

确定数据是可用的,还是可以转换为指定的格式。 标志 Boolean 指示是否可检查数据是否可以转换为指定格式(如果该格式不可用)。

public:
 virtual bool GetDataPresent(System::String ^ format, bool autoConvert);
public bool GetDataPresent(string format, bool autoConvert);
abstract member GetDataPresent : string * bool -> bool
override this.GetDataPresent : string * bool -> bool
Public Function GetDataPresent (format As String, autoConvert As Boolean) As Boolean

参数

format
String

指定要检查的数据格式的字符串。 有关一组预定义的数据格式,请参阅该 DataFormats 类。

autoConvert
Boolean

false (仅检查指定格式); true 若要检查此数据对象中存储的数据是否可以转换为指定的格式,

返回

true 如果数据处于指定格式或可转换为,则为否则,为 false.

实现

例外

formatnull

示例

以下示例使用此方法通过描述符字符串查询数据,并指定如何处理自动转换数据格式。

DataObject dataObject = new DataObject("Some string data to store...");

// Query for the presence of Text data in the data object, by data format descriptor string,
// and specifying whether auto-convertible data formats are acceptable.  

// In this case, Text data is present natively, so GetDataPresent returns "true".
string textData = null;
if (dataObject.GetDataPresent(DataFormats.Text, false /* Auto-convert? */))
{
    textData = dataObject.GetData(DataFormats.Text) as string;
}

// In this case, the Text data in the data object can be autoconverted to 
// Unicode text, but it is not available natively, so GetDataPresent returns "false".
byte[] unicodeData = null;
if (dataObject.GetDataPresent(DataFormats.UnicodeText, false /* Auto-convert? */))
{
    unicodeData = dataObject.GetData(DataFormats.UnicodeText) as byte[];
}

// In this case, the Text data in the data object can be autoconverted to 
// Unicode text, so GetDataPresent returns "true".
if (dataObject.GetDataPresent(DataFormats.UnicodeText, true /* Auto-convert? */))
{
    unicodeData = dataObject.GetData(DataFormats.UnicodeText) as byte[];
}
Dim dataObject As New DataObject("Some string data to store...")

' Query for the presence of Text data in the data object, by data format descriptor string,
' and specifying whether auto-convertible data formats are acceptable.  

' In this case, Text data is present natively, so GetDataPresent returns "true".
Dim textData As String = Nothing
If dataObject.GetDataPresent(DataFormats.Text, False) Then ' Auto-convert? 
    textData = TryCast(dataObject.GetData(DataFormats.Text), String)
End If

' In this case, the Text data in the data object can be autoconverted to 
' Unicode text, but it is not available natively, so GetDataPresent returns "false".
Dim unicodeData() As Byte = Nothing
If dataObject.GetDataPresent(DataFormats.UnicodeText, False) Then ' Auto-convert? 
    unicodeData = TryCast(dataObject.GetData(DataFormats.UnicodeText), Byte())
End If

' In this case, the Text data in the data object can be autoconverted to 
' Unicode text, so GetDataPresent returns "true".
If dataObject.GetDataPresent(DataFormats.UnicodeText, True) Then ' Auto-convert? 
    unicodeData = TryCast(dataObject.GetData(DataFormats.UnicodeText), Byte())
End If

注解

调用 GetDataPresent 以确定格式是否在此数据对象中可用,然后再调用 GetData。 调用 GetFormats 以获取此数据对象中提供的所有格式的列表。

另请参阅

适用于