如何在C#中VBA.Collection()对象对象、如何在、VBA、Collection

2023-09-03 05:22:31 作者:我爱的王冷静他不冷静i

我需要创建一个从C#code 互操作VBA.Collection对象 我必须提及Interop.VBA在我的项目

I need create Interop VBA.Collection object from C# code I have reference to Interop.VBA in my project

当我打电话说:

var col = new VBA.Collection()

在运行时我有一个错误,指出DLL未注册... 我发现: http://support.microsoft.com/kb/323737/en-us

In runtime I've got an error saying that dll is not registered... I found that: http://support.microsoft.com/kb/323737/en-us

这可能工作,但我没有VB6编译器对我的盒子。 我不知道你知道其他的解决办法(或也许有人可以编译此ActiveX对我?)

It might work but I don't have VB6 compiler on my box. I wonder you know other workaround (or maybe someone can compile this ActiveX to me?)

推荐答案

我没有尝试这样做,但它可能工作。

I haven't tried this, but it might work.

创建一个导入库VB6的VBA6.dll。创建自己的实现其_Collection接口。使用替代VBA.Collection类的此实现。

Create an import library for VB6's VBA6.dll. Create your own implementation of its _Collection interface. Use this implementation in place of the VBA.Collection class.

class MyCollection : VBA._Collection
{
    private Dictionary<object, object> _items = new Dictionary<object, object>();

    public void Add(ref object Item, [System.Runtime.InteropServices.OptionalAttribute]ref object Key, [System.Runtime.InteropServices.OptionalAttribute]ref object Before, [System.Runtime.InteropServices.OptionalAttribute]ref object After)
    {
        // Ignoring the Before and After params for simplicity
        _items.Add(Key, Item);
    }

    public int Count()
    {
        return _items.Count;
    }

    public System.Collections.IEnumerator GetEnumerator()
    {
        return _items.Values.GetEnumerator();
    }

    public dynamic Item(ref object Index)
    {
        return _items[Index];
    }

    public void Remove(ref object Index)
    {
        _items.Remove(Index);
    }
}