DataObject.GetData 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
以指定資料格式回傳資料。
多載
| 名稱 | Description |
|---|---|
| GetData(String) |
回傳資料格式為字串指定。 |
| GetData(Type) |
回傳由物件指定的 Type 格式的資料物件。 |
| GetData(String, Boolean) |
回傳指定格式的資料物件,並可選擇將資料轉換為指定格式。 |
GetData(String)
回傳資料格式為字串指定。
public:
virtual System::Object ^ GetData(System::String ^ format);
public object GetData(string format);
public object? GetData(string format);
abstract member GetData : string -> obj
override this.GetData : string -> obj
Public Function GetData (format As String) As Object
參數
- format
- String
一個指定資料格式的字串。 關於一組預先定義的資料格式,請參見類別 DataFormats 。
傳回
一個包含指定格式資料的物件,或 null 當資料無法以指定格式取得時。
實作
例外狀況
format 為 null。
範例
以下範例使用此方法,首先檢查指定資料格式是否可用(原生或自動轉換);若有指定格式,範例會透過該 GetData(String) 方法檢索資料。
DataObject dataObject = new DataObject("Some string data to store...");
string desiredFormat = DataFormats.UnicodeText;
byte[] data = null;
// Use the GetDataPresent method to check for the presence of a desired data format.
// This particular overload of GetDataPresent looks for both native and auto-convertible
// data formats.
if (dataObject.GetDataPresent(desiredFormat))
{
// If the desired data format is present, use one of the GetData methods to retrieve the
// data from the data object.
data = dataObject.GetData(desiredFormat) as byte[];
}
Dim dataObject As New DataObject("Some string data to store...")
Dim desiredFormat As String = DataFormats.UnicodeText
Dim data() As Byte = Nothing
' Use the GetDataPresent method to check for the presence of a desired data format.
' This particular overload of GetDataPresent looks for both native and auto-convertible
' data formats.
If dataObject.GetDataPresent(desiredFormat) Then
' If the desired data format is present, use one of the GetData methods to retrieve the
' data from the data object.
data = TryCast(dataObject.GetData(desiredFormat), Byte())
End If
以下範例程式碼使用該 GetDataPresent(String, Boolean) 方法,首先檢查指定的資料格式是否原生可用(自動轉換的資料格式會被過濾);若指定格式可用,範例會透過該 GetData(String) 方法取得資料。
DataObject dataObject = new DataObject("Some string data to store...");
string desiredFormat = DataFormats.UnicodeText;
bool noAutoConvert = false;
byte[] data = null;
// Use the GetDataPresent method to check for the presence of a desired data format.
// The autoconvert parameter is set to false to filter out auto-convertible data formats,
// returning true only if the specified data format is available natively.
if (dataObject.GetDataPresent(desiredFormat, noAutoConvert))
{
// If the desired data format is present, use one of the GetData methods to retrieve the
// data from the data object.
data = dataObject.GetData(desiredFormat) as byte[];
}
Dim dataObject As New DataObject("Some string data to store...")
Dim desiredFormat As String = DataFormats.UnicodeText
Dim noAutoConvert As Boolean = False
Dim data() As Byte = Nothing
' Use the GetDataPresent method to check for the presence of a desired data format.
' The autoconvert parameter is set to false to filter out auto-convertible data formats,
' returning true only if the specified data format is available natively.
If dataObject.GetDataPresent(desiredFormat, noAutoConvert) Then
' If the desired data format is present, use one of the GetData methods to retrieve the
' data from the data object.
data = TryCast(dataObject.GetData(desiredFormat), Byte())
End If
另請參閱
適用於
GetData(Type)
回傳由物件指定的 Type 格式的資料物件。
public:
virtual System::Object ^ GetData(Type ^ format);
public object GetData(Type format);
public object? GetData(Type format);
abstract member GetData : Type -> obj
override this.GetData : Type -> obj
Public Function GetData (format As Type) As Object
參數
- format
- Type
A Type 用來指定資料格式。 關於一組預先定義的資料格式,請參見類別 DataFormats 。
傳回
資料物件以指定格式呈現,或 null 當資料無法以指定格式取得時。
實作
例外狀況
format 為 null。
另請參閱
適用於
GetData(String, Boolean)
回傳指定格式的資料物件,並可選擇將資料轉換為指定格式。
public:
virtual System::Object ^ GetData(System::String ^ format, bool autoConvert);
public object GetData(string format, bool autoConvert);
public object? GetData(string format, bool autoConvert);
abstract member GetData : string * bool -> obj
override this.GetData : string * bool -> obj
Public Function GetData (format As String, autoConvert As Boolean) As Object
參數
- format
- String
一個指定資料格式的字串。 關於一組預先定義的資料格式,請參見類別 DataFormats 。
- autoConvert
- Boolean
true 嘗試自動將資料轉換成指定格式; false 無需資料格式轉換。
傳回
資料物件以指定格式呈現,或 null 當資料無法以指定格式取得時。
若參數為true且資料無法轉換為指定格式,或自動轉換被禁用(將SetData(String, Object, Boolean)autoConvert參數設為 false呼叫),則此方法回傳 null。autoConvert
實作
例外狀況
format 為 空值。