DataObject.GetData 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳與指定資料格式相關的資料。
多載
| 名稱 | Description |
|---|---|
| GetData(String, Boolean) |
回傳與指定資料格式相關的資料,並使用自動轉換參數判斷是否將資料轉換為該格式。 |
| GetData(String) |
回傳與指定資料格式相關的資料。 |
| GetData(Type) |
回傳與指定類別類型格式相關的資料。 |
GetData(String, Boolean)
回傳與指定資料格式相關的資料,並使用自動轉換參數判斷是否將資料轉換為該格式。
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
要取回的資料格式。 請參閱 DataFormats 預設格式。
- autoConvert
- Boolean
true將資料轉換為指定格式;否則,。 false
傳回
與指定格式相關的資料,或 null。
實作
範例
以下程式碼範例利用參數指定是否轉換資料格式,取得儲存在 中DataObjectautoConvert的資料。
首先,會用文字資料建立一個新 DataObject 檔案。 接著範例嘗試擷取資料,將格式指定為字串且不需格式轉換,也就是 autoConvert 參數為 false。 此操作失敗是因為 中 DataObject沒有字串資料。
接著,範例嘗試再次取得資料,參數 autoConvert 設為 true。 此操作成功後,結果會以 。MessageBox
這套規範要求已經建立。textBox1
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
備註
如果 autoConvert 參數是, true 且此方法找不到指定格式的資料,則嘗試將資料轉換成該格式。 若資料無法轉換為指定格式,或資料儲存時自動轉換設定為 false,則此方法回傳 null。
若 autoConvert 參數為 false,則此方法回傳指定格式的資料,若 null 找不到該格式的資料。
要判斷資料是否與某種格式相關聯或可轉換成格式,請先GetData呼叫 GetDataPresent 。 請呼叫 GetFormats 一份存於此 DataObject資料的有效格式清單。
Note
如果資料儲存時明確允許轉換,且所請求的格式與儲存格式相容,則可轉換成其他格式。 例如,儲存在 Unicode 中的資料可以轉換成文字。
當 format 變為 Html 時,此方法在目標 .NET 4.5 或更高解析度的應用中回傳 UTF-8 編碼字串,在目標 .NET 4.0 或更低的應用中回傳 ANSI 編碼字串。
另請參閱
適用於
GetData(String)
回傳與指定資料格式相關的資料。
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
要取回的資料格式。 請參閱 DataFormats 預設格式。
傳回
與指定格式相關的資料,或 null。
實作
範例
以下程式碼範例取回儲存在 DataObject. 首先,會用文字資料建立一個新 DataObject 檔案。 接著,資料會被檢索,並以字串形式指定,並顯示在文字框中。
這套規範要求已經建立。textBox1
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
備註
若此方法找不到指定格式的資料,則嘗試將資料轉換成該格式。 若資料無法轉換為指定格式,或資料儲存時自動轉換為 false,則此方法會回傳 null。
要判斷資料是否與某種格式相關聯或可轉換成格式,請先GetData呼叫 GetDataPresent 。 請呼叫 GetFormats 一份存於此 DataObject資料的有效格式清單。
Note
如果資料儲存時明確允許轉換,且所請求的格式與儲存格式相容,則可轉換成其他格式。 例如,儲存在 Unicode 中的資料可以轉換成文字。
當 format 變為 Html 時,此方法在目標 .NET 4.5 或更高解析度的應用中回傳 UTF-8 編碼字串,在目標 .NET 4.0 或更低的應用中回傳 ANSI 編碼字串。
另請參閱
適用於
GetData(Type)
回傳與指定類別類型格式相關的資料。
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
參數
傳回
與指定格式相關的資料,或 null。
實作
範例
以下程式碼範例取回儲存在 DataObject. 首先,會用一個元件建立一個新元件 DataObject 。 接著,資料會被檢索並指定其類型。 檢索資料的類型會顯示在文字框中。
這套規範要求已經建立。textBox1
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
備註
若此方法找不到指定格式的資料,則嘗試將資料轉換成該格式。 若資料無法轉換為指定格式,或資料儲存時自動轉換為 false,則此方法會回傳 null。
要判斷資料是否與某種格式相關聯或可轉換成格式,請先GetData呼叫 GetDataPresent 。 請呼叫 GetFormats 一份存於此 DataObject資料的有效格式清單。
Note
如果資料儲存時明確允許轉換,且所請求的格式與儲存格式相容,則可轉換成其他格式。 例如,儲存在 Unicode 中的資料可以轉換成文字。