개체를 T로 캐스팅


91

XmlReader.NET 의 클래스로 XML 파일을 구문 분석하고 있으며 일반적으로 다른 특성을 읽는 일반 구문 분석 함수를 작성하는 것이 현명하다고 생각했습니다. 다음과 같은 기능을 생각해 냈습니다.

private static T ReadData<T>(XmlReader reader, string value)
{
    reader.MoveToAttribute(value);
    object readData = reader.ReadContentAsObject();
    return (T)readData;
}

내가 깨달았을 때, 이것은 내가 계획 한대로 완전히 작동하지 않습니다. 이 같은 기본 형식에 오류가 발생 int또는 double캐스트는 변환 할 수 없기 때문에, string숫자 입력합니다. 내 기능이 수정 된 형태로 우세 할 수있는 방법이 있습니까?

답변:


208

먼저 캐스팅 할 수 있는지 확인합니다.

if (readData is T) {
    return (T)readData;
} 
try {
   return (T)Convert.ChangeType(readData, typeof(T));
} 
catch (InvalidCastException) {
   return default(T);
}

1
Convert.ChangeType의 줄을 'return (T) Convert.ChangeType (readData, typeof (T), System.Globalization.CultureInfo.InstalledUICulture.NumberFormat)으로 변경하여 다양한 문화 구성에서 작동하도록했습니다.
Kasper Holdum 2009

2
이것이 정답입니다. 그러나 여기서 try / catch가 완전히 중복된다는 주장을 할 수 있습니다. 특히 음소거 된 예외를 고려합니다. 나는 if (readData is T) {...} 부분이 충분한 시도라고 생각합니다.
pim

readDate를 변환하기 전에 null인지 확인할 수 있습니다. 그렇다면 default (T)를 반환합니다.
Manuel Koch

"개체가 IConvertible을 구현해야합니다."라는 메시지가 나타납니다.
Casey Crookston

19

Convert.ChangeType 을 사용해 보셨습니까 ?

메서드가 항상 이상한 문자열을 반환하지만 요점을 벗어난다면이 변경된 코드는 원하는대로 수행 할 수 있습니다.

private static T ReadData<T>(XmlReader reader, string value)
{
    reader.MoveToAttribute(value);
    object readData = reader.ReadContentAsObject();
    return (T)Convert.ChangeType(readData, typeof(T));
}

처음에는 Convert.ChangeType을 살펴 보았지만 이상한 이유로이 작업에 유용하지 않다고 결정했습니다. 당신과 Bob은 모두 같은 대답을 제공했고, 나는 당신의 대답을 혼합하기로 결정했기 때문에 try 문 사용을 피했지만 가능하면 여전히 'return (T) readData'를 사용했습니다.
Kasper Holdum

11

시험

if (readData is T)
    return (T)(object)readData;

3

유형이 참조 유형이되도록 요구할 수 있습니다.

 private static T ReadData<T>(XmlReader reader, string value) where T : class
 {
     reader.MoveToAttribute(value);
     object readData = reader.ReadContentAsObject();
     return (T)readData;
 }

그런 다음 값 유형과 TryParse를 사용하는 다른 작업을 수행하십시오.

 private static T ReadDataV<T>(XmlReader reader, string value) where T : struct
 {
     reader.MoveToAttribute(value);
     object readData = reader.ReadContentAsObject();
     int outInt;
     if(int.TryParse(readData, out outInt))
        return outInt
     //...
 }

3

실제로 여기서 문제는 ReadContentAsObject를 사용하는 것입니다. 불행히도이 방법은 기대에 부응하지 못합니다. 값에 가장 적합한 유형을 감지해야하지만 실제로는 문자열을 반환합니다 (Reflector를 사용하여 확인할 수 있음).

그러나 특정 경우에는 캐스팅하려는 유형을 이미 알고 있으므로 잘못된 방법을 사용하고 있다고 말할 수 있습니다.

대신 ReadContentAs를 사용해보십시오. 정확히 필요한 것입니다.

private static T ReadData<T>(XmlReader reader, string value)
{
    reader.MoveToAttribute(value);
    object readData = reader.ReadContentAs(typeof(T), null);
    return (T)readData;
}

매우 작고 우아하게 보입니다. 그러나 허용 된 답변의 솔루션은 IFormatProvider를 허용하기 때문에 여러 다른 문화권과 호환되는 ChangeType을 사용합니다. 이것이 프로젝트의 필수 요소이므로 해당 솔루션을 계속 사용할 것입니다.
Kasper Holdum 2009

2

아마도 문자열에서 T로 변환 할 델리게이트를 매개 변수로 전달할 수 있습니다.


1

'클래스'제약 조건을 추가합니다 (또는 실행 된 T 개체의 기본 클래스 또는 인터페이스와 같이보다 상세 함).

private static T ReadData<T>(XmlReader reader, string value) where T : class
{
    reader.MoveToAttribute(value);
    object readData = reader.ReadContentAsObject();
    return (T)readData;
}

또는 where T : IMyInterface또는 where T : new()


1

실제로 응답은 흥미로운 질문을 불러 일으키는데, 오류 발생시 함수가 수행하기를 원하는 것입니다.

T로 읽으려고 시도하지만 수행 할 수없는 경우 false를 반환하는 TryParse 메서드 형식으로 구성하는 것이 더 합리적일까요?

    private static bool ReadData<T>(XmlReader reader, string value, out T data)
    {
        bool result = false;
        try
        {
            reader.MoveToAttribute(value);
            object readData = reader.ReadContentAsObject();
            data = readData as T;
            if (data == null)
            {
                // see if we can convert to the requested type
                data = (T)Convert.ChangeType(readData, typeof(T));
            }
            result = (data != null);
        }
        catch (InvalidCastException) { }
        catch (Exception ex)
        {
            // add in any other exception handling here, invalid xml or whatnot
        }
        // make sure data is set to a default value
        data = (result) ? data : default(T);
        return result;
    }

편집 : 이제 생각 해보니 convert.changetype 테스트를해야하나요? as 라인이 이미 그렇게하려고하지 않습니까? 추가 변경 유형 호출을 수행하는 것이 실제로 어떤 일을 수행하는지 확신하지 못합니다. 실제로 예외를 생성하여 처리 오버 헤드를 증가시킬 수 있습니다. 누군가가 할 가치가있는 차이점을 알고 있다면 게시하십시오!

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.