【发布时间】:2010-04-04 08:19:01
【问题描述】:
好的,我有几个要在 VBA 中使用的 .NET 类。所以我必须通过 COM 注册它们。我想我(终于)弄清楚了 COM 注册,但现在我需要有关如何创建对象的语法的帮助。这是一些伪代码,显示了我正在尝试做的事情。
编辑:将附加对象更改为返回 ArrayList 而不是 List
.NET 类看起来像这样...
public class ResourceManagment
{
public ResourceManagment()
{
// Default Constructor
}
public static List<RandomObject> AttachedObjects()
{
ArrayList list = new ArrayList();
return list;
}
}
public class RandomObject
{
//
public RandomObject(int someParam)
{
}
}
好的,这就是我想在 VBA 中做的事情(在 C# 中演示),但我不知道如何......
public class VBAClass
{
public void main()
{
ArrayList myList = ResourceManagment.AttachedObjects();
foreach(RandomObject x in myList)
{
// Do something with RandomObject x like list them in a Combobox
}
}
}
需要注意的一点是 RandomObject 没有公共的默认构造函数。所以我不能像Dim x As New RandomObject 这样创建它的实例。 MSDN 说你不能通过 COM 实例化一个没有默认构造函数的对象,但是如果它是由另一个方法返回的,你仍然可以使用该对象类型... Types must have a public default constructor to be instantiated through COM. Managed, public types are visible to COM. However, without a public default constructor (a constructor without arguments), COM clients cannot create an instance of the type. COM clients can still use the type if the type is instantiated in another way and the instance is returned to the COM client. You may include overloaded constructors that accept varying arguments for these types. However, constructors that accept arguments may only be called from managed (.NET) code.
补充:这是我在VB中的尝试:
Dim count As Integer
count = 0
Dim myObj As New ResourceManagment
For Each RandomObject In myObj.AttachedObjects
count = count + 1
Next RandomObject
【问题讨论】:
标签: .net vba com instantiation