I need to iterate COM+/ActiveX collection objects using late bind interop in C#. At this very moment I need to iterate COMAdmin.COMAdminCatalogCollection
, from GetCollection("Applications")
method in COMAdmin.COMAdminCatalog
. But as it is a POC to be used with others proprietary COM+/ActiveX objects I need to get this done with late bound. How I should box my object
object to be iterable?
COMPlus.cs
public abstract class COMPlus
{
public object COMObject { get; private set; }
public System.Type COMObjectType { get; private set; }
protected COMPlus(string progId)
{
this.COMObject = System.Activator.CreateInstance(System.Type.GetTypeFromProgID(progId));
this.COMObjectType = this.COMObject.GetType();
}
protected COMPlus(object comObject, string progId)
{
this.COMObject = comObject;
this.COMObjectType = System.Type.GetTypeFromProgID(progId);
}
}
COMAdminCatalog.cs
public class COMAdminCatalog : COMPlus
{
public COMAdminCatalog() : base("COMAdmin.COMAdminCatalog") { }
public COMAdminCatalog(object comObject) : base(comObject, "COMAdmin.COMAdminCatalog") { }
public void Connect(string serverAddress)
{
}
public COMAdminCatalogCollection GetCollection(string collectionName)
{
return new COMAdminCatalogCollection(
base.COMObjectType.InvokeMember("GetCollection",
System.Reflection.BindingFlags.InvokeMethod,
null,
base.COMObject,
new object[] { (object)collectionName }));
}
}
COMAdminCatalogCollection.cs
public class COMAdminCatalogCollection : COMPlus
{
public COMAdminCatalogCollection() : base("COMAdmin.COMAdminCatalog") { }
public COMAdminCatalogCollection(object comObject) : base(comObject, "COMAdmin.COMAdminCatalog") { }
public void Populate()
{
base.COMObjectType.InvokeMember("Populate",
System.Reflection.BindingFlags.InvokeMethod,
null,
base.COMObject, null);
}
}
Toolbox.cs
public static class Toolbox
{
public static void CreateApp(string appName, string serverAddress = null)
{
COMAdminCatalog comAdminCatalog = new Interop.COMAdmin.COMAdminCatalog();
COMAdminCatalogCollection comAdminCatalogCollection;
if (!String.IsNullOrEmpty(serverAddress))
{
comAdminCatalog.Connect(serverAddress);
}
comAdminCatalogCollection = comAdminCatalog.GetCollection("Applications");
comAdminCatalogCollection.Populate();
// here the fun has to begin iterating the Applications collection to verify if there is already an application with the given name or not.
}
}
EDIT
I need it compatible with .Net 2.0 (3.5 tops), so dynamic don't suits me.