首页 > 解决方案 > RegAsm.exe 遇到问题

问题描述

目前我正在遵循这个程序:

  1. 创建一些 C# 类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace TestAgain
{
    public class Test
    {
        void testIt() {
            MessageBox.Show("Yellow world");
        }
    }
}

  1. 签署项目。
  2. 在 AssemblyInfo.cs 中设置[assembly: ComVisible(false)][assembly: ComVisible(true)].
  3. 构建项目。
  4. 运行 regasm

"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe" -tlb -codebase "C:\Users\sancarn\Desktop\tbd\TestAgain\TestAgain\bin\Debug\TestAgain.dll" -verbose

regasm 的输出如下:

Microsoft .NET Framework Assembly Registration Utility version 4.7.3056.0
for Microsoft .NET Framework version 4.7.3056.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Types registered successfully
Type 'TestAgain.Test' exported.
Assembly exported to 'C:\Users\sancarn\Desktop\tbd\TestAgain\TestAgain\bin\Debug\TestAgain.tlb', and the type library was registered successfully

所以类型TestAgain.Test被导出并且类型库已经被注册。伟大的!

所以我去 Excel-VBA 并使用以下方法对其进行测试:

Sub testIt()
    Dim o As Object
    Set o = CreateObject("TestAgain.Test")
    Call o.testIt
End Sub

并得到错误Error 429: ActiveX component can't create object。IE 没注册?

有没有人知道为什么这不起作用?


注意:我之前已经设法获得了一个带有接口的 Com-Visible 应用程序,但我一直在寻找一种更简洁的方法,一位用户告诉我 regasm 可以完成这项工作。

标签: c#.netcom

解决方案


我已经知道这一点,但它溜走了我的脑海:

  • 只有公共方法对 COM 公开。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace TestAgain
{
    public class Test
    {
        public void testIt() {
            MessageBox.Show("Yellow world");
        }
    }
}

这就像一个魅力。


推荐阅读