使用COM-互操作传递从VBA到C#数组 [英] Pass an array from vba to c# using com-interop

查看:159
本文介绍了使用COM-互操作传递从VBA到C#数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是使用COM的互操作通过用户定义的类的数组从VBA到.NET(特别是C#)的正确方法?



下面是我的C#代码。如果我把方法一从VBA它与数组或用户自定义类型预期的失败或功能使用Visual Basic中不支持自动化类型。

 公共类MyClass的
{
公共方法一(UserDefinedClass []参数){...}
酒店的公共方法二(Object参数){...}
}

我读过一点关于MarshallAsAttribute类。难道这是在C#代码中缺少的部分?



下面是我使用的VBA代码:

 暗淡UDT作为新UserDefinedClass 
尺寸myArray的()
myArray的(1)= UDT
myClass.Method1(myarray的)
myClass.Method2(myarray的)


解决方案

IIRC你必须通过引用传递数组。



尝试声明你的方法

 公共类MyClass的
{
公共无效方法1([在]参考UserDefinedClass []参数){...}

}

如果你不想污染与.NET客户端ref参数类,你可以定义一个标记有ComVisible特性接口由COM客户端使用,并实现它明确这样的:

  [标记有ComVisible特性(真)] 
公共接口IMyClass
{
无效方法1([IN]参考UserDefinedClass []参数){ ...}

}

公共MyClass类:IMyClass
{
无效IMyClass.Method1(参考UserDefinedClass []参数)
{
this.Method1(参数);
}

公共方法一(UserDefinedClass []参数)
{

}
}

**在回应置评**
如果你想要公开的集合,而不是数组VBA,你只需要公开你想要的VBA代码能够调用一个枚举,以及任何其他方法(如添加,删除,插入,清除,...)。例如。

  [标记有ComVisible特性] 
公共接口IUserDefinedClassCollection
{
的IEnumerator的GetEnumerator();

诠释计数{搞定; };

IUserDefinedClass这个[INT指数] {搞定; }

INT加入(IUserDefinedClass项目);

//等等,其他的方法,如删除,清除,...
}

然后,您可以使用它像往常一样在VBA:

 昏暗objUserDefinedClasses作为UserDefinedClassCollection 
..
objUserDefinedClasses.Add objUserDefinedClass

。对于参数nIndex = 0至objUserDefinedClasses.Count

下一参数nIndex


What is the proper way to pass an array of user defined classes from vba to .net (specifically c#) using com-interop?

Here's my c# code. If I call Method1 from vba it's failing with "Array or userdefined type expected" or "Function uses an automation type not supported in visual basic".

public class MyClass 
{
    public Method1(UserDefinedClass[] Parameters) { ... }
    public Method2(Object Parameters) { ... }
}

I've read a bit about the MarshallAsAttribute class. Could this be the missing piece in the c# code?

Here's the vba code I'm using:

Dim udt As New UserDefinedClass
Dim myArray()
myArray(1) = udt
myClass.Method1(myArray)
myClass.Method2(myArray)

解决方案

IIRC you have to pass arrays by reference.

Try declaring your method as

public class MyClass  
{ 
    public void Method1([In] ref UserDefinedClass[] Parameters) { ... } 
    ...
} 

If you don't want to pollute your class with ref parameters for .NET clients, you can define a ComVisible interface to be used by COM clients, and implement it explicitly thus:

[ComVisible(true)]
public interface IMyClass  
{ 
    void Method1([In] ref UserDefinedClass[] Parameters) { ... } 
    ...
} 

public class MyClass : IMyClass
{
    void IMyClass.Method1(ref UserDefinedClass[] Parameters)
    {
        this.Method1(Parameters);
    }

    public Method1(UserDefinedClass[] Parameters)
    {
        ...
    }
}

** In response to comment ** If you want to expose a collection instead of an array to VBA, you just need to expose an enumerator, and any other methods you want the VBA code to be able to call (e.g. Add, Remove, Insert, Clear, ...). E.g.

[ComVisible]
public interface IUserDefinedClassCollection
{
    IEnumerator GetEnumerator();

    int Count { get; };

    IUserDefinedClass this[int index] { get; }

    int Add(IUserDefinedClass item);

    // etc, other methods like Remove, Clear, ...
}

You can then use it as usual in VBA:

Dim objUserDefinedClasses As UserDefinedClassCollection
...
objUserDefinedClasses.Add objUserDefinedClass 
...
For nIndex = 0 To objUserDefinedClasses.Count

Next nIndex

这篇关于使用COM-互操作传递从VBA到C#数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆