从VBA访问.NET通用对象 [英] Access .NET generic objects from VBA

查看:44
本文介绍了从VBA访问.NET通用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.net代码具有一个具有许多通用属性的对象.该对象返回到VBA代码.所有非通用属性都运行良好,但是我还需要访问通用值.是否可以通过VBA做到这一点?

My .net code has an object with a number of generic properties. This object is returned to the VBA code. All the non-generic properties are working well, but I also need to access the generic values. Is there a way to do it from VBA?

[ClassInterface(ClassInterfaceType.AutoDual)]
public class Obj
{
    public string GetProp1() {...}
    public IList<MyCustomType> GetProp2() {...}
}

VB代码:

Sub Test()
    Dim o As Program.Obj
    Set o = New Program.Obj
    Set p2 = hp.GetProp2()

    set p2_0 = p2(0) ' doesn't work

End Sub

推荐答案

我发现本文描述了一个丑陋的骇客-通过反思调用所有内容.由于仍返回一个通用值作为对象,因此反射应该可以工作,但会造成以下问题:1)速度慢,2)容易出错,3)调用非常不便.

I found this article describing an ugly hack - calling everything through reflection. Since a generic value is still returned as an object, reflection should work, but will be 1) slow, 2) error prone, and 3) very inconvenient to call.

如文章所示,我将使用具有以下签名的实用程序方法:

As the article shows, I would use utility methods with the following signatures:

public object GetProperty(object Instance, string Property)
public object InvokeMethod(object Instance, string Method)
public object InvokeMethod_OneParm(object Instance, string Method, object Parm1)
public object InvokeMethod_TwoParms(object Instance, string Method, object Parm1, object Parm2)
public void SetProperty(object Instance, string Property, object Value)

丑陋,但可行.

这篇关于从VBA访问.NET通用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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