Microsoft MVP성태의 닷넷 이야기
Reflection 관련 질문 드립니다. [링크 복사], [링크+제목 복사],
조회: 8370
글쓴 사람
양승조 donator
홈페이지
첨부 파일
 

안녕하세요. 선생님 Reflection 관련 질문 드립니다.

클래스 내에 여러가지 프로퍼티를 선언하여 사용하고 있고, 그 프로퍼티들을 아래와 같이 접근하여 사용하고 있습니다.
일반 프로퍼티는 SetValue나 GetValue 모두 접근이 가능합니다.
문제는 List 형태의 Property에 접근시 문제가 됩니다.
아래는 코드 입니다.

{
    var props = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
    for(int i = 0; i < props.Length; i++)
    {
        string nameofType = this.GetType().Name;
        string name = props[i].Name;
        string type = props[i].GetType().ToString();

        if (typeof(System.Collections.IList).IsAssignableFrom(props[i].PropertyType))
        {
            //var list = props[i].GetValue(props[i]) as IEnumerable;
            //List<bool> another = props[i].GetValue(props[i]) as List<bool>;
        }
        else
        {
            object readValue2 = Convert.ChangeType(readValue, props[i].PropertyType);
            props[i].SetValue(this, readValue2);
        }
    }
}

if문에서 리스트 타입인지 확인 후 아래에서 GetValue로 List를 받아 처리하려고 합니다.
여러가지 검색내용을 적용해 보았고, 위 코드에서 if문안에 주석처리된 부분과 같이 적용해 보았으나,
아래와 같은 예외가 발생합니다.

Exception thrown: 'System.Reflection.TargetException' in System.Private.CoreLib.dll
An unhandled exception of type 'System.Reflection.TargetException' occurred in System.Private.CoreLib.dll
Object does not match target type.

감사합니다.








[최초 등록일: ]
[최종 수정일: 4/24/2024]


비밀번호

댓글 작성자
 



2024-04-24 11시38분
저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압축해서 올려주세요.
정성태

... 61  62  63  64  [65]  66  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
979왕초보7/1/201117972Apache + IIS Redirect [2]
9776/16/201122277웹컨트롤 어느거 쓰는게 좋나요 [1]
976박성준6/13/201117769VS2008 Add-in 구현 관련 질문 [4]
972김길6/6/201120038메모리 해제 예외 처리 관련.. [2]
971강동원5/29/201117560firebird install건 [1]
970임동찬5/18/201118352ASP.net 솔루션 디버깅 관련 [1]
969이성환5/4/201120024WMI 를 사용하지 않고 하드웨어 정보를 가져올 수 없을까요? [3]
968김동미4/28/201119435안녕하세요 다시 한번 문의를 드립니다.. [2]파일 다운로드1
967임동찬4/22/201122179C# using문 관련 [9]
964김동미4/18/201119779wcf IsOneWay 속성관련 문의 입니다..
965정성태4/18/201121151    답변글 [답변]: wcf IsOneWay 속성관련 문의 입니다..
966김동미4/19/201118980        답변글 [답변]: [답변]: wcf IsOneWay 속성관련 문의 입니다.. [1]
963최재훈4/12/201117363wcf inactivityTimeout 설정시 문의 사항이 있습니다. [2]
962임동찬4/8/201116981TFS 사용관련 [1]
961임동찬4/7/201117337XSD & XML & XmlCodeGenerator [2]
960임동찬4/5/201118866XML Schema Editor [4]
959immm3/24/201116668로그인 연동 어려운 건가요? [1]
958꼭지3/3/201118699Supporting compressed request in WCF 3.5 [5]
957임동찬2/21/201119209WCF channel faulted 관련 [2]
956윤용한2/18/201122649WaitHandle.WaitOne 과 Stopwatch에 관한 질문 [3]
955최광욱2/17/201119312TFS 에서 소스 영구 제거 방법 [1]
954한장우2/16/201117035atl activeX 질문이요~ [1]
952박용운2/16/201117897IE8.0에서 BHO [1]
953박용운2/16/201117896    답변글 [답변]: IE8.0에서 BHO
951임동찬2/11/201117926WCF Service Reference [1]
950이성환2/9/201119384Windows application 프로젝트를 참조 했을 때 생성되는 실행파일을 직접 실행 불가능하도록 하고 싶습니다. [6]파일 다운로드1
... 61  62  63  64  [65]  66  67  68  69  70  71  72  73  74  75  ...