Archives / 2004 / September
  • Updated ComPlusWrapper class

    Tags: .NET, C#

    Here is an updated version of the ComPlusWrapper that I previously published, this one contains a Hashtable for caching the ProgId/Types as well as a method for invoking methods.Please post any comments to the code or any improvments to the class.

    public class ComPlusWrapper : IDisposable { private static volatile Hashtable s_types = new Hashtable();private static volatile Object s_lock = new Object();private object _ComPlusObject; private Type _type;private bool _IsDisposed = false;

    public ComPlusWrapper(string ProgId) { if(s_types.ContainsKey(ProgId)) {_type = s_types[ProgId] as Type;} else {lock(s_lock) {_type = Type.GetTypeFromProgID(ProgId, true); s_types.Add(ProgId, _type);}} _ComPlusObject = System.Activator.CreateInstance(_type); }

    ~ComPlusWrapper() {this.Dispose();}public object ComPlusObject{get {if(this._IsDisposed) {throw new ObjectDisposedException(null);}return _ComPlusObject;} }

    public Type ComPlusType {get {if(this._IsDisposed) {throw new ObjectDisposedException(null);}return _type;}}

    public void Dispose() { if(!this._IsDisposed) {IDisposable dispose = _ComPlusObject as IDisposable; if(dispose != null) { dispose.Dispose(); } else {System.Runtime.InteropServices.Marshal.ReleaseComObject(_ComPlusObject);}_type = null;_ComPlusObject = null;} GC.SuppressFinalize(this);this._IsDisposed = true;}

    public object InvokeMember(string Name, object[] Arguments) {return _type.InvokeMember(Name, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.IgnoreCase, null, _ComPlusObject, Arguments); }}

About Wictor...

Wictor Wilén is the Nordic Digital Workplace Lead working at Avanade. Wictor has achieved the Microsoft Certified Architect (MCA) - SharePoint 2010, Microsoft Certified Solutions Master (MCSM) - SharePoint  and Microsoft Certified Master (MCM) - SharePoint 2010 certifications. He has also been awarded Microsoft Most Valuable Professional (MVP) for seven consecutive years.

And a word from our sponsors...