How do I handle outlooks VSTO objects as if they are mailitems
I have written some VSTO (2003) code that sucessfully applies some mandatory subject line changes according to attachements of mail items. The code is written to operate on microsoft.office.interop.outlook.mailitem However, I need the same code to operate on other types such as microsoft.office.interop.outlook.appointmentitem for instance(infact it needs to work for anything the user can send that would have a subject).
Assuming the outlook item types have a subject property, an attachments property and a save method, how can I approach writing code that works for al开发者_StackOverflowl relevant interop.outlook types.
I tried addressing this via reflection but GetProperty is returing null so I can't use GetValue on it
? mi.GetType().GetProperty("Subject") null ?(mi as Microsoft.Office.Interop.Outlook.MailItem).Subject "Test Subject"
there doesn't seem to be a generic outlookitem class I can cast to, to do this. What's the correct approach ?
EDIT: To clarif my code starts like this...
void Application_ItemSend(object Item, ref bool Cancel) { if (Item is Microsoft.Office.Interop.Outlook.MailItem) { Microsoft.Office.Interop.Outlook.MailItem currentItem = Item as Microsoft.Office.Interop.Outlook.MailItem;
then does stuff to currentItem (including passing to to various functions currently typed with Microosft.Office.Interop.Outlook.MailItem properties. I want them to handle "Microsoft.Interop.Outlook.somethingsendable"
That's not going to work out of the box - AppointmentItem
and MailItem
are completely different interfaces, so polymorphism is not an option.
The best I can suggest is that you create a SendableItem
class of your own to wrap the PIA interfaces you need to support, and encapsulate the switching code there behind a common wrapper for the 'common' operations you want to use. You would create a SendableItem using either a MailItem
or an AppointmentItem
but once created, they look the same from the outside of the SendableItem
wrapper.
精彩评论