Iterate ActiveX collection object with late bind interop on C# (COMAdminCatalogCollection) -


i need iterate com+/activex collection objects using late bind interop in c#. @ moment need iterate comadmin.comadmincatalogcollection, getcollection("applications") method in comadmin.comadmincatalog. poc used others proprietary com+/activex objects need done late bound. how should box object object 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 fun has begin iterating applications collection verify if there application given name or not.     } } 

edit

i need compatible .net 2.0 (3.5 tops), dynamic don't suits me.

you use foreach keyword in c#, iterate collection. dlr knows how automagically map icatalogcollection::_newenum() method.

let's first early-bound version since unlikely want late-bound. server has been stable long time , .net 4.0 embed interop types feature avoid dependency on interop library. use project > add reference > browse button > select c:\windows\system32\com\comadmin.dll. sample code:

static void earlybound(string server) {     var cat = new comadmin.comadmincatalog();     cat.connect(server);     var coll = (comadmin.comadmincatalogcollection)cat.getcollection("applications");     coll.populate();     foreach (comadmin.icatalogobject app in coll) {         console.writeline(app.name);         var comps = coll.getcollection("components", app.key);         comps.populate();         foreach (comadmin.icatalogobject comp in comps) {             console.writeline("  {0} - {1}", comp.name, comp.key);         }     } } 

the late-bound version isn't different dynamic keyword:

static void latebound(string server) {     var catt = type.gettypefromprogid("comadmin.comadmincatalog");     dynamic cat = activator.createinstance(catt);     cat.connect(server);     dynamic coll = cat.getcollection("applications");     coll.populate();     foreach (dynamic app in coll) {         console.writeline(app.name);         dynamic comps = coll.getcollection("components", app.key);         comps.populate();         foreach (dynamic comp in comps) {             console.writeline("  {0} - {1}", comp.name, comp.key);         }     } } 

as demanded, late-bound version of code .net 2.0, cruel , unusual punishment that's outlawed geneva convention on programmer's rights. obtain iterator _newenum() com method, can cast ienumerator:

static void latebound20(string server) {     type catt = type.gettypefromprogid("comadmin.comadmincatalog");     object cat = activator.createinstance(catt);     cat.gettype().invokemember("connect", bindingflags.invokemethod, null,         cat, new object[] { server });     object coll = cat.gettype().invokemember("getcollection", bindingflags.invokemethod, null,         cat, new object[] { "applications" });     coll.gettype().invokemember("populate", bindingflags.invokemethod, null,         coll, new object[] { });     object iter = coll.gettype().invokemember("_newenum", bindingflags.invokemethod, null,         coll, new object[] { });     system.collections.ienumerator iter1 = (system.collections.ienumerator)iter;     while (iter1.movenext()) {         object app = iter1.current;         object name = app.gettype().invokemember("name", bindingflags.getproperty, null,             app, new object[] { });         object key = app.gettype().invokemember("key", bindingflags.getproperty, null,             app, new object[] { });         console.writeline(name.tostring());         object comps = coll.gettype().invokemember("getcollection", bindingflags.invokemethod, null,             coll, new object[] { "components", key });         comps.gettype().invokemember("populate", bindingflags.invokemethod, null,             comps, new object[] { });         object iter2 = comps.gettype().invokemember("_newenum", bindingflags.invokemethod, null,             comps, new object[] { });         system.collections.ienumerator iter3 = (system.collections.ienumerator)iter2;         while (iter3.movenext()) {             object comp = iter3.current;             object cname = comp.gettype().invokemember("name", bindingflags.getproperty, null,                 comp, new object[] { });             object ckey = comp.gettype().invokemember("key", bindingflags.getproperty, null,                 comp, new object[] { });             console.writeline("  {0} - {1}", cname, ckey);         }     } } 

output on com+ unencumbered machine 3 snippets:

com+ utilities   remotehelper.remotehelper - {e423af7c-fc2d-11d2-b126-00805fc73204}   txctx.transactioncontext - {7999fc25-d3c6-11cf-acab-00a024a55aef}   txctx.transactioncontextex - {5cb66670-d3d4-11cf-acab-00a024a55aef}   qc.recorder - {ecabafc2-7f19-11d2-978e-0000f8757e2a}   qc.listenerhelper - {ecabafc4-7f19-11d2-978e-0000f8757e2a} com+ qc dead letter queue listener   qc.dlqlistener - {ecabafca-7f19-11d2-978e-0000f8757e2a} com+ explorer   comexps.ctrkevntlistener - {2c3e140b-7a0d-42d1-b2aa-d343500a90cf} com+ utilities (32 bit)   remotehelper.remotehelper - {e423af7c-fc2d-11d2-b126-00805fc73204}   txctx.transactioncontext - {7999fc25-d3c6-11cf-acab-00a024a55aef}   txctx.transactioncontextex - {5cb66670-d3d4-11cf-acab-00a024a55aef}   qc.recorder - {ecabafc2-7f19-11d2-978e-0000f8757e2a}   qc.listenerhelper - {ecabafc4-7f19-11d2-978e-0000f8757e2a} system application   catsrv.catalogserver - {182c40f0-32e4-11d0-818b-00a0c9231c29}   eventpublisher.eventpublisher - {ecabafbc-7f19-11d2-978e-0000f8757e2a}   comsvcs.trackerserver - {ecabafb9-7f19-11d2-978e-0000f8757e2a}   mts.mtsgrp - {4b2e958d-0393-11d1-b1ab-00aa00ba3258}   pdump.processdump - {ecabb0c4-7f19-11d2-978e-0000f8757e2a} 

Comments