Share via


방법: 데이터 개체에 데이터 형식이 있는지 확인

업데이트: 2007년 11월

다음 예제에서는 다양한 GetDataPresent 메서드 오버로드를 사용하여 데이터 개체에 특정 데이터 형식이 있는지 쿼리하는 방법을 보여 줍니다.

예제

설명

다음 예제 코드에서는 GetDataPresent(String) 오버로드를 사용하여 설명자 문자열을 기준으로 특정 데이터 형식이 있는지 쿼리하는 방법을 보여 줍니다.

코드

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 false.
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[];
}

예제

설명

다음 예제 코드에서는 GetDataPresent(Type) 오버로드를 사용하여 형식을 기준으로 특정 데이터 형식이 있는지 쿼리하는 방법을 보여 줍니다.

코드

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;
}

예제

설명

다음 예제 코드에서는 GetDataPresent(String, Boolean) 오버로드를 사용하여 설명자 문자열을 기준으로 데이터를 쿼리하고 자동 변환할 수 있는 데이터 형식의 처리 방법을 지정합니다.

코드

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[];
}

참고 항목

참조

IDataObject

기타 리소스

끌어서 놓기 샘플