【问题标题】:tlb generates no methodstlb 不生成任何方法
【发布时间】:2015-11-26 06:20:44
【问题描述】:

我创建了一个类库 DLL 并将其打包,但是当我在 VBA (Excel) 中使用它时,它没有任何方法。来自另一个 .NET 程序的一切都很好:

这是我的代码:

    namespace NeilLibrary
    {
        public interface INeilTest
        {
            string DoMethodReturn(string name);
            string DoMethodOut(string name, out string name2, out bool isPrev);
        }
        [Guid("BA7CC0F2-9C07-4EF9-B799-18D317B7E293")]
        [ComVisible(true)]
        public class NeilTest
        {
            public NeilTest() { }
            [ComVisible(true)]
            public string DoMethodReturn(string name)
            {
                return "name: " + name;
            }

            [ComVisible(true)]
            public string DoMethodOut(string name, out string name2, out bool isPrev)
            {
                name2 = "New Value";
                isPrev = true;
                return "Name: " + name;
            }
        }        }

我在程序集中启用 COM 的情况下构建了它,并运行了以下命令:

regasm.exe NeilLibrary.dll /tlb:neil.tlb

效果很好。在 Excel 中,我导入了 tlb,它出现在对象浏览器中,因此创建正常,但没有方法。虽然显示了接口,但也显示了它的方法,但这并没有多大用处。

有人可以帮忙吗。

我添加了无参数构造函数,因为我在某处读到有类似问题的人必须这样做。

【问题讨论】:

  • 你试过thisthis吗?
  • 请阅读我在您其他帖子中的回复.....stackoverflow.com/questions/33923624/…
  • COM 类型没有“构造函数”,这就是为什么如果托管类型只有一个参数化构造函数,你就不能将它暴露给 COM - 实际上你可以,但是您的 COM 客户端将无法实例化它。 ...在你碰壁之前,你也不能将泛型类型暴露给 COM。 =)

标签: .net vba com


【解决方案1】:

哦,我看到了不同之处在于您现在发布了您的代码....

您需要使要使用 COM 的功能可见。您可以在属性文件中或在您的类上方执行此操作

[ComVisible(true)]
[Guid("blah")]
public class ConnectionUtilities: IConnectionUtilities
{
}

通过进入 C# 项目的属性->AssemblyInfo.cs 并像这样声明,尝试使整个程序集 ComVisible:

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0a2d43c9-da5e-4a59-8006-ff131b33d86c")]

【讨论】:

  • 谢谢,我已经更新了我上面的代码,但仍然不开心。它是否需要接口和接口类型等设置? tbh,我认为这将是一项简单的工作,但我找不到任何可以帮助您完成此任务的内容。
  • @NeilWalker 你让你的方法可见,但你的班级不可见。你不应该需要任何其他的东西。
  • 还是不行。您的程序集详细信息已经存在,我可以看到类,只是看不到方法,并且我添加了一个接口,这对 VBA 中的方法可见,而不是类。谢谢你的耐心:)
【解决方案2】:

首先感谢大家的帮助。

最终解决方案相当简单。我只需将“/codebase”添加到 regasm.exe(或对其签名并通过 gacutil 添加)。

另外,我没有检查任何其他排列,但这是它对我有用的唯一方法:

namespace NeilLibrary
    {
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface INeilTest
        {
            [DispId(1)]
            string DoMethodReturn(string name);
            [DispId(2)]
            string DoMethodOut(string name, out string name2, out bool isPrev);
        }

        [Guid("BA7CC0F2-9C07-4EF9-B799-18D317B7E293")]
        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.None)]
        [ProgId("NeilLibrary.NeilTest")]
        public class NeilTest : INeilTest
        {
            [ComVisible(true)]
            public string DoMethodReturn(string name)
            {
                return "name: " + name;
            }

            [ComVisible(true)]
            public string DoMethodOut(string name, out string name2, out bool isPrev)
            {
                name2 = "New Value";
                isPrev = true;
                return "Name: " + name;
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多