I need to iterate collections of COM + / ActiveX objects with interop using latebound in C #. Right now my need is to iterate through the collection of ActiveX objects COMAdmin.COMAdminCatalogCollection
, return of method GetCollection("Applications")
of component COMAdmin.COMAdminCatalog
. But at the moment it is a POC that will be used with other internal development COM + / ActiveX components, and so I need to develop this solution using late bound. How should I box the 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();
// aqui deve começar a brincadeira iterando a coleção de aplicações pra verificar se a aplicação solicitada já existe ou não.
}
}