Condividi tramite


Procedura: determinare se un formato di dati è presente in un oggetto dati

Negli esempi seguenti viene illustrato come utilizzare i vari GetDataPresent overload del metodo per eseguire una query sul fatto che un particolare formato di dati sia presente in un oggetto dati.

Overload GetDataPresent(String)

Descrizione

Il codice di esempio seguente usa l'overload GetDataPresent(String) per eseguire una query sulla presenza di un particolare formato di dati in base alla stringa del descrittore.

Codice

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

Overload GetDataPresent(Type)

Descrizione

Il codice di esempio seguente usa l'overload GetDataPresent(Type) per eseguire una query sulla presenza di un particolare formato di dati per tipo.

Codice

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

Overload GetDataPresent(String, Boolean)

Descrizione

Il codice di esempio seguente usa l'overload GetDataPresent(String, Boolean) per eseguire query sui dati in base alla stringa del descrittore e specificando come gestire i formati di dati convertibili automaticamente.

Codice

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

Vedi anche