(1) The 'least code' way is to not cast the outlook type unless you have to, as this will work wherever the item shared common properties and/or methods i.e. use a 'helper' function to call the COM object with a late bound method, like this:
"string subject = (string) OLLateBoundPropertyGetHelper(cItem, "Subject");
public object OLLateBoundPropertyGetHelper(object targetObject, string propertyName)
{
return(targetObject.GetType().InvokeMember(propertyName,
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.GetProperty,
null,
targetObject,
null,
CultureInfo.CurrentCulture));
}"
(2) The other way is to test the type, but without using an exception, i.e. something like this:
"GenericSaveItem(cItem);
public void GenericSaveItem(object item)
{
if (item is MailItem)
{
MailItem mi = item as MailItem;
mi.Save() ;
}
else if (item is AppointmentItem)
{
AppointmentItem ai = item as AppointmentItem;
ai.Save() ;
... and so on - there are about 15 types."
For more details let me know, or grab a copy of something like 'VSTO ISBN:0-321-33488-4' ( http://tinyurl.com/qyc9c ) as it explains better than I could.
David |