如何在 C# 中创建 VBA.Collection() 对象 [英] How to create VBA.Collection() object in c#
问题描述
我需要从 C# 代码创建 Interop 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);
}
}
这篇关于如何在 C# 中创建 VBA.Collection() 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!