【发布时间】:2011-02-19 06:26:00
【问题描述】:
我正在使用 Exchange Web 服务托管 API 以及联系人数据。我有以下代码,功能性,但并不理想:
foreach (Contact c in contactList)
{
string openItemUrl = "https://" + service.Url.Host + "/owa/" + c.WebClientReadFormQueryString;
row = table.NewRow();
row["FileAs"] = c.FileAs;
row["GivenName"] = c.GivenName;
row["Surname"] = c.Surname;
row["CompanyName"] = c.CompanyName;
row["Link"] = openItemUrl;
//home address
try { row["HomeStreet"] = c.PhysicalAddresses[PhysicalAddressKey.Home].Street.ToString(); }
catch (Exception e) { }
try { row["HomeCity"] = c.PhysicalAddresses[PhysicalAddressKey.Home].City.ToString(); }
catch (Exception e) { }
try { row["HomeState"] = c.PhysicalAddresses[PhysicalAddressKey.Home].State.ToString(); }
catch (Exception e) { }
try { row["HomeZip"] = c.PhysicalAddresses[PhysicalAddressKey.Home].PostalCode.ToString(); }
catch (Exception e) { }
try { row["HomeCountry"] = c.PhysicalAddresses[PhysicalAddressKey.Home].CountryOrRegion.ToString(); }
catch (Exception e) { }
//and so on for all kinds of other contact-related fields...
}
正如我所说,这段代码有效。如果可能的话,现在我想让它少一点。
我找不到任何方法可以让我在尝试访问它之前检查字典中的键是否存在,如果我尝试读取它(使用.ToString())并且它不存在那么抛出异常:
500
字典中没有给定的键。
我怎样才能重构这段代码以减少吸收(同时仍然有效)?
【问题讨论】:
标签: c# exchangewebservices ews-managed-api