Can I call a static method of a C# class from VBA via COM?

爱⌒轻易说出口 提交于 2019-12-17 20:35:59

问题


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.

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


回答1:


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.

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.




回答2:


COM does not support static methods.

http://msdn.microsoft.com/en-us/library/ms182198.aspx



来源:https://stackoverflow.com/questions/24193183/can-i-call-a-static-method-of-a-c-sharp-class-from-vba-via-com

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!