我可以通过COM从VBA调用C#类的静态方法吗? [英] Can I call a static method of a C# class from VBA via COM?

查看:94
本文介绍了我可以通过COM从VBA调用C#类的静态方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在C#/。NET类库中定义一个类,则通过使其可见COM,可以实例化该类并使用COM从VBA调用其方法。

If I define a class in a C#/.NET class library, then by making it COM visible I can instantiate the class and call its methods from VBA using COM.

是否可以从VBA调用此类的 static 方法?

Is there any way to call the static methods of such a class from VBA?

推荐答案

COM不支持静态方法,并且COM对象的实例不调用静态方法。而是在静态方法上设置 ComVisible(false),然后创建一个实例方法来包装它:

COM does not support static methods, and instances of COM objects do not invoke static methods. Instead, set ComVisible(false) on your static method, then make an instance method to wrap it:

[ComVisible(true)]
public class Foo
{
    [ComVisible(false)]
    public static void Bar() {}

    public void BarInst()
    {
        Bar();
    }
}

或者只是使方法实例而不是静态实例而忘记

Or just make the method instance instead of static and forget static all together.

您没有必须将静态方法标记为对COM不可见,但是它满足某些代码分析工具的要求会警告您有关COM可见类型的静态方法,并明确指出该静态方法不适用于COM。

You don't have to mark the static method as not visible to COM, however it satisfies some code analysis tools that would warn you about static methods on COM visible types, and makes it clear that the static method is not intended to be visible to COM.

这篇关于我可以通过COM从VBA调用C#类的静态方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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