DataObject.GetData 方法

定義

以指定的資料格式傳回資料。

多載

GetData(String)

以字串指定的格式傳回資料。

GetData(Type)

Type 物件指定的格式傳回資料物件。

GetData(String, Boolean)

以指定的格式傳回資料物件,並選擇性地將資料轉換為指定的格式。

GetData(String)

以字串指定的格式傳回資料。

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

傳回

Object

物件,包含指定之格式的資料,如果資料無法使用指定的格式,則傳回 null

實作

例外狀況

formatnull

範例

下列範例會使用這個方法,先檢查指定的資料格式是否 (原生或自動轉換) ;如果指定的格式可用,則此範例會使用 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);
abstract member GetData : Type -> obj
override this.GetData : Type -> obj
Public Function GetData (format As Type) As Object

參數

format
Type

指定資料格式的 Type。 如需一組預先定義的資料格式,請參閱 DataFormats 類別。

傳回

Object

包含所指定格式之資料的資料物件,如果資料無法使用指定的格式,則會傳回 null

實作

例外狀況

formatnull

另請參閱

適用於

GetData(String, Boolean)

以指定的格式傳回資料物件,並選擇性地將資料轉換為指定的格式。

public:
 virtual System::Object ^ GetData(System::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 表示不進行資料格式轉換。

傳回

Object

包含所指定格式之資料的資料物件,如果資料無法使用指定的格式,則會傳回 null

如果 autoConvert 參數為 true,而資料無法轉換為指定的格式,或已停用自動轉換 (在 autoConvert 參數為 false 的情況下呼叫 SetData(String, Object, Boolean)),則這個方法會傳回 null

實作

例外狀況

formatnull

另請參閱

適用於