C# 搜索不存在的对象名称时检测错误

C# 搜索不存在的对象名称时检测错误,c#,object,C#,Object,在下面的代码中,当对象HWRes.hwresbj中不存在MyGlobals.ListOfItemsToControl[i].sItemName时,我希望在不跳到catch语句的情况下检测此问题 我怎样才能做到这一点 try { String HWTemp = ""; // Ref http://stackoverflow.com/questions/15628140/c-sharp-eliminate-switch-requirement HWTemp = HWRes.H

在下面的代码中,当对象
HWRes.hwresbj
中不存在MyGlobals.ListOfItemsToControl[i].sItemName时,我希望在不跳到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
    .....
}

检查
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
}