Iterate Activex object collection with latebound Interop in c# (Command)

Asked

Viewed 71 times

0

I need to iterate collections of COM+/Activex objects with latebound in C#. Right now my need is to iterate the collection of Activex objects COMAdmin.COMAdminCatalogCollection, return of the method GetCollection("Applications") of the component COMAdmin.COMAdminCatalog. But at the moment it is a POC that will be used with other proprietary COM+/Activex components, internal development, and so I need to develop this solution using late bound. How should I make Boxing the object object to be everlasting?

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.
    }
}
  • I didn’t understand the question but still this suggestion might be useful: instead of using Object or even the imported component interface, use the type Dynamic to declare the variable that will receive the reference to Activex. This makes life much easier and according to some is even more performatic for interaction .Net with COM+/Activex.

  • Dynamic is only available from . Net 4, I need backward compatibility with 2.0, which allows me to go up to 3.5 at most.

1 answer

0


Browser other questions tagged

You are not signed in. Login or sign up in order to post.