问题描述
好吧,我快疯了.我一直在尝试使一个简单的.net 程序集可见以用作 COM 对象,但它不起作用.我一直在尝试很多不同的方法,但都没有奏效.到目前为止,我得到的最接近的是属性、接口、手动注册等数量最少的那个.
Ok, I'm going crazy. I've been trying to make a simple .net assembly visible to use as a COM object but it's not working. I've been trying a lot of different aproaches but none of them work. So far the closest i've got is the one with the least amount of attributes, interfaces, manually registering etc.
我有一个 vbscript 作为测试,看起来像这样:
I have a vbscript as a test which looks like this:
Dim o
o = CreateObject("MyNamespace.MyClass")
msgbox o.Test()
我的 c# 代码如下所示:
My c# code looks like this:
using System;
using System.Runtime.InteropServices;
namespace MyNamespace
{
[ProgId("MyNamespace.MyClass")]
public class MyClass
{
public string Test()
{
return "teststring";
}
}
}
在我的项目 (VS2008) 中,我选择了使程序集 COM 可见" - 它向我的 AssemblyInfo.cs 添加了一个属性,并且我还检查了注册 COM 互操作".
On my project (VS2008) i've selected 'Make assembly COM visible' - which added an attribute to my AssemblyInfo.cs and i also checked 'Register for COM interop'.
我可以看到 ProgId 确实没有什么区别(在我的情况下)——只要我指定真正的命名空间+类名,我可以覆盖它,我认为这很好.
I Can see that the ProgId really makes no difference (in my case anyways) - as long as i specify the real namespace+classname, i can however override it which i guess is nice.
问题是 - 我的 CreateObject 运行良好,但找不到方法 Test().事实上,如果我获取 dll 并提取 tlb 并期望我根本看不到任何方法.
The problem is - my CreateObject goes well, but the method Test() can not be found. In fact if i take the dll and extract the tlb and expect that i see no methods at all.
我得到的错误是这样的:
The error i get is this:
C:inetpubwwwrootASPTest estrun.vbs(3,1) Microsoft VBScript 运行时错误:对象不支持此属性或方法:'测试'
C:inetpubwwwrootASPTest estrun.vbs(3, 1) Microsoft VBScript runtime error: Object doesn't support this property or method: 'Test'
我希望得到任何反馈,但只是为了让您知道我一直在尝试什么.我在类上搞乱了 Guid 和 AttributeGuid,在类上明确定义了 COMVisible 我尝试不注册 COM 互操作,而是做了 regasm/codebase/regfile.
I'd love any feedback, but just to let you know what i've been trying. I've messing around with Guid and AttributeGuid on the class, defining the COMVisible explicitly on the class as well i tried NOT registering for COM interop and instead did regasm /codebase /regfile.
最后一点 - 我的大多数测试都围绕一个未签名的程序集展开,但试图查明我尝试使用强名称/密钥的错误,但它没有做任何事情.
One last note - most my testing has revolved around a non-signed assembly, but trying to pinpoint the error i tried with a strongname/key as well but it didn't do anything.
谁能帮我找出问题?
推荐答案
我从头开始,在我的项目中删除了有关 COM 互操作的任何内容,然后我添加了
I started from the beginning and removed anything regarding COM interop on my project, i then added
[Guid(...some guid...)]
[ComVisible(true)]
在 MyClass 上,我添加了一个强名称(使用项目设置来设置它 - 而不是在 AssemblyInfo.cs 中定义它.
on MyClass and i added a strongname (used the project setting to set it - instead of defining it in the AssemblyInfo.cs.
当我编译程序集时,我将文件复制到另一个我做的地方
When i compiled the assembly, i copied the files to another location where i did a
regasm MyNamespace.MyClass.dll /codebase /regfile
然后我运行并接受了注册表项.
Then i ran and accepted the registry entry.
这使它起作用了-每当我进行更改时,我只需重新编译并复制旧文件(无需取消注册),然后我只做:
This made it work - whenever i make changes i just recompile and copy the files over the old (no unregistering) and then i only do:
regasm MyNamespace.MyClass.dll /codebase
...就是这样 - 我对 MyClass 所做的更改在我的 vbscript 和我的 asp 页面中立即可见.
... and that's it - changes i made to MyClass are imediately visible in my vbscript and in my asp page.
这篇关于为 COM 互操作性注册我的 .net 程序集不会公开我的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!