DataObject.GetDataPresent 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
判斷資料是否以指定格式提供或可轉換。
多載
| 名稱 | Description |
|---|---|
| GetDataPresent(String) |
判斷資料是否可用或可轉換為由字串指定的格式。 |
| GetDataPresent(Type) |
判斷資料是否可用或可轉換為物件 Type 指定的格式。 |
| GetDataPresent(String, 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
實作
例外狀況
format 為 null。
範例
以下範例使用此方法,透過描述符字串查詢特定資料格式的存在。
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
實作
例外狀況
format 為 null。
範例
以下範例使用此方法,依類型查詢特定資料格式的存在。
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
實作
例外狀況
format 為 null。
範例
以下範例使用此方法透過描述符字串查詢資料,並說明如何處理可自動轉換的資料格式。
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 取得此資料物件中所有可用格式的清單。