Add to My Yahoo! | Google Reader or Homepage | Add to Windows Live | Add to Windows Live Alerts

Wictor Wilén

Microsoft Certified Master (MCM) - SharePoint 2010 | Microsoft Most Valuable Professional (MVP) - SharePoint Server MVP | Author

Updated ComPlusWrapper class

Posted at 2004-09-02 07:14 by Wictor Wilén in .NET , C# with 1 comments.

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);
}
}

Comments and trackbacks

#  A ComPlusWrapper class by Trackback
Screenshot from websnpr You've been kicked (a good thing) - Trackback from DotNetKicks.com
Make a comment on this post:
Subject:  

Your name:  
Your Url:  
Note: submissions may have to be approved before being visible, so don't submit your comment multiple times.