VBA导入COM注册的dll并使用参数调用构造函数 [英] VBA importing COM-registered dll and calling constructor with arguments
问题描述
是否可以引用COM注册的DLL,然后在VBA代码中创建在构造函数中需要参数的对象?
Is it possible to reference a COM-registered DLL and then create objects that require arguments in the constructor in VBA code?
我已经成功引用了tlb使用 regasm
注册的Access 2013(64位),我设法创建了一个不需要构造函数中参数的简单对象。
I'm successfully referenced the tlb in Access 2013 (64-bit) that was registered with regasm
, and I've managed to create a simple object that doesn't need arguments in the constructor.
根据我的阅读,事实并非如此。现在我要面对主要重构吗?
From what I read, it is not. Do I now face major refactoring?
推荐答案
它不必是主要。您只需要一个工厂方法,该方法接受构造对象所需的所有参数。例如:
It doesn't have to be "major". You simply need a factory method, one that takes all the arguments you need to construct the object. For example:
public class Foo {
internal Foo(int arg1, string arg2) {
// etc...
}
// Other methods and properties
//...
}
public class FooFactory {
public Foo Create(int arg1, string arg2) {
return new Foo(arg1, arg2);
}
}
现在,您只需在中使用FooFactory.Create()您的VBA代码。
And now you simply use FooFactory.Create() in your VBA code.
这篇关于VBA导入COM注册的dll并使用参数调用构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!