2013-04-12 1 views
0

MyGlobals.ListOfItemsToControl[i].sItemName이 개체 HWRes.HWResObj에없는 경우 아래 코드에서 catch 문으로 이동하지 않고이 문제를 감지하고 싶습니다.존재하지 않는 개체 이름을 검색하는 동안 오류를 검색하는 중

어떻게하면됩니까?

try 
{ 
    String HWTemp = ""; 
    // Ref http://stackoverflow.com/questions/15628140/c-sharp-eliminate-switch-requirement 
    HWTemp = HWRes.HWResObj.GetType().GetProperty(MyGlobals.ListOfItemsToControl[i].sItemName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(HWRes.HWResObj, null).ToString(); 


// Somehow here I should detect if the value MyGlobals.ListOfItemsToControl[i].sItemName does not exist in the object HWRes.HWResObj 
// Detect issue without jumping to catch 

} 
catch 
{ 
    // I dont want to go here when MyGlobals.ListOfItemsToControl[i].sItemName does not exist in the object HWRes.HWResObj 
    ..... 
} 

답변

2

확인과 같은 GetProperty의 반환 값 :

var property = HWRes.HWResObj.GetType().GetProperty(MyGlobals.ListOfItemsToControl[i].sItemName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); 
if (property != null) 
{ 
    string HWTemp = property.GetValue(HWRes.HWResObj, null).ToString(); 
} 
else 
{ 
    // property does not exist 
} 
관련 문제