2014-07-14 1 views
0

리플렉션을 사용하여 nullable 부울 값을 처리하려고합니다. 값은 DB에서 오므로 동일하게 유지해야합니다.리플렉션을 사용하여 Nullable 부울 값 가져 오기

다음은 현재 사용중인 코드입니다.

내가 bool? tempBool = (bool?)prop.GetValue(prop, null);에서 브레이크 포인트를 넣어 라인을 실행하면
public partial class PrinterConfigUC : UserControl 
{ 
    prtsetup Printer { get; set; } 

    public PrinterConfigUC(prtsetup printer) 
    { 
     InitializeComponent(); 
     this.Printer = printer; 
     lblPrinterName.Text = Printer.prtname; 

     var properties = printer.GetType().GetProperties();   

     foreach (var prop in properties) 
     { 
      //In debug, a nullable bool had a type name of "Nullable`1" 
      if (prop.PropertyType.Name.Equals("Nullable`1")) 
      { 
       bool? tempBool = (bool?)prop.GetValue(prop, null); 
      } 
     } 
    } 

, 프로그램 추가 실행을 중지하고 그냥 나에게 빈의 WinForm을 보여줍니다. 다른 일은 일어나지 않습니다. 오류 메시지가없고 프로그램이 중단되지도 않고 한 줄에 멈춰 있습니다.

+2

그 줄은'bool이 아니어야합니까? tempBool = (bool?) prop.GetValue (printer);'? – DavidG

+0

DavidG가 맞습니다. 내가 할 수있는 한 아래의 답을 확인해 줄 것입니다. 도와 주셔서 감사합니다. 내 벙어리 실수. –

답변

0

변경이 :

bool? tempBool = (bool?)prop.GetValue(prop, null); 

하려면 :

bool? tempBool = (bool?)prop.GetValue(printer, null); 

GetValue의 첫 번째 인수는 메타 데이터입니다 위의 예에서 프린터가 아닌 숙박 시설입니다 소스입니다.

+0

댓글에 쓴 내용은 무엇입니까 ... – DavidG

+0

@DavidG 죄송합니다. 게시했을 때 귀하의 댓글을 보지 못했습니다 ... –

관련 문제