using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace LBOutlookLibrary { public abstract partial class LBComObject { private Object _Item; internal Object Item { get { return _Item; } set { _Item = value; if (value != null) _MyType = _Item.GetType(); } } private Type _MyType; public Type MyType { get { return _MyType; } set { _MyType = value; } } protected LBComObject() { } protected LBComObject(string ProgID) { Type objClassType; objClassType = Type.GetTypeFromProgID(ProgID); Item = Activator.CreateInstance(objClassType); } protected LBComObject(Object item) { Item = item; } private Object DoInvokeMember(string name, Object[] parameters, BindingFlags bf, Binder b) { try { return _MyType.InvokeMember(name, bf, b, _Item, parameters); } catch (Exception ex) { throw new Exception(string.Format("LBComObject.DoInvokeMember {0}.{1}", _MyType.Name, name), ex); } return null; } protected void SetProperty(string propertyName, params Object[] parameters) { DoInvokeMember(propertyName, parameters, BindingFlags.SetProperty, null); } protected Object GetProperty(string propertyName) { return DoInvokeMember(propertyName, null, BindingFlags.GetProperty, null); } protected Object GetProperty(string propertyName, params Object[] parameters) { return DoInvokeMember(propertyName, parameters, BindingFlags.GetProperty, null); } protected Object InvokeMethod(string methodName, params Object[] parameters) { if (parameters != null) FixParameters(parameters); return DoInvokeMember(methodName, parameters, BindingFlags.InvokeMethod, null); } private void FixParameters(object[] parameters) { // Loop through parameters checking to make sure that none of them are LBComObjects for (int i = 0; i < parameters.Length; i++) if (parameters[i] is LBComObject) parameters[i] = (parameters[i] as LBComObject).Item; } protected Object InvokeMethod(string methodName) { return InvokeMethod(methodName, null); } } public class LBComObjectList : LBComObject where TList : LBComObjectList where TItem : LBComObject, new() { //public new(Object item):base(item){} public TItem Add() { TItem tmp = new TItem(); tmp.Item = InvokeMethod("Add"); return tmp; } //public TItem this[int item] //{ // get // { // TItem tmp = new TItem(); // tmp.Item = GetProperty("Item", item); // return tmp; // } //} } public partial class LBApplicationClass : LBComObject { public LBApplicationClass() : base("Outlook.Application") { } public LBApplicationClass(Object item) : base(item) { } public System.Object CreateItem(LBOlItemType ItemType) { return InvokeMethod("CreateItem", ItemType); } public void Quit() { InvokeMethod("Quit"); } } public enum LBOlItemType { olMailItem = 0, olAppointmentItem = 1, olContactItem = 2, olTaskItem = 3, olJournalItem = 4, olNoteItem = 5, olPostItem = 6, olDistributionListItem = 7 } public partial class LBMailItem : LBComObject { public LBMailItem() { } public LBMailItem(Object item) : base(item) { } } public partial class LBMailItemClass : LBComObject { public LBMailItemClass() { } public LBMailItemClass(Object item) : base(item) { } public String Body { get { return (GetProperty("Body").ToString()); } set { SetProperty("Body", value); } } public LBOlBodyFormat BodyFormat { get { return (LBOlBodyFormat)GetProperty("BodyFormat"); } set { SetProperty("BodyFormat", value); } } public LBAttachments Attachments { get { return new LBAttachments(GetProperty("Attachments")); } } public String Subject { get { return (GetProperty("Subject").ToString()); } set { SetProperty("Subject", value); } } public LBRecipients Recipients { get { return new LBRecipients(GetProperty("Recipients")); } } public String To { get { return (GetProperty("To").ToString()); } set { SetProperty("To", value); } } public void Send() { InvokeMethod("Send"); } } public enum LBOlBodyFormat { olFormatUnspecified = 0, olFormatPlain = 1, olFormatHTML = 2, olFormatRichText = 3 } public partial class LBAttachments : LBComObject { public LBAttachments() { } public LBAttachments(Object item) : base(item) { } public int Count { get { return (GetProperty("Count") as int? ?? 0); } } public LBAttachment Item { get { return new LBAttachment(GetProperty("Item")); } } public LBAttachment Add(object Source) { return new LBAttachment(InvokeMethod("Add", Source, Missing.Value, Missing.Value, Missing.Value)); } public LBAttachment Add(object Source, object Type, object Position, object DisplayName) { return new LBAttachment(InvokeMethod("Add", Source, Type, Position, DisplayName)); } public void Remove(int Index) { InvokeMethod("Remove", Index); } } public partial class LBRecipients : LBComObject { public LBRecipients() { } public LBRecipients(Object item) : base(item) { } public int Count { get { return (GetProperty("Count") as int? ?? 0); } } public LBRecipient Item { get { return new LBRecipient(GetProperty("Item")); } } public LBRecipient Add(string Name) { return new LBRecipient(InvokeMethod("Add", Name)); } public void Remove(int Index) { InvokeMethod("Remove", Index); } public Boolean ResolveAll() { return InvokeMethod("ResolveAll") as Boolean? ?? false; } } public partial class LBAttachment : LBComObject { public LBAttachment() { } public LBAttachment(Object item) : base(item) { } public LBOlAttachmentType Type { get { return (LBOlAttachmentType)GetProperty("Type"); } } } public partial class LBRecipient : LBComObject { public LBRecipient() { } public LBRecipient(Object item) : base(item) { } public String Address { get { return (GetProperty("Address").ToString()); } } public String Name { get { return (GetProperty("Name").ToString()); } } public Boolean Resolve() { return InvokeMethod("Resolve") as Boolean? ?? false; } } public enum LBOlAttachmentType { olByValue = 1, olByReference = 4, olEmbeddeditem = 5, olOLE = 6 } }