c# – .NET Interop:在当前目录中使用程序集

我试图通过interop从VB6使用.NET程序集而不将其放在GAC中,而不使用regasm.exe的/ codebase参数.

根据我的理解,当我在.NET类库上运行regasm.exe时,它会为类库中的每个类创建一个注册表项,告诉COM客户端他们应该加载mscoree.dll作为包装.NET对象的代理. COM使用. Mscoree.dll在类的注册表中使用InprocServer32 / Assembly键来确定哪个类库包含该类的实现.

如果我使用/ codebase和regasm.exe或将我的类库放在GAC中,一切正常;但据我从散乱的文档中可以看出,mscoree.dll应该在当前目录和路径中寻找汇编,如果/ codebase没有被使用(因此注册表中没有CodeBase条目)它在GAC中找不到它.

C#代码尽可能简单:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace myinterop
{
    [Guid("B1D6B9FE-A4C7-11DD-B06B-E93056D89593")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class MyServer
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}

问题再现了一个VBScript一个衬垫,我把它放在与编译的DLL相同的目录中:

object = CreateObject("myinterop.MyServer")

我在这里错过了什么?有没有明确的描述mscoree.dll在某处查找程序集的方式?

顺便说一句,我使用的是.NET 2.0,是的,我知道我应该将我的程序集放在GAC中,我只是想知道为什么这不像宣传的那样有效.

解决方法:

MSDN开始:

/Codebase : Creates a Codebase entry in the
registry. The Codebase entry specifies
the file path for an assembly that is
not installed in the global assembly
cache. You should not specify this
option if you will subsequently
install the assembly that you are
registering into the global assembly
cache. The assemblyFile argument that
you specify with the /codebase option
must be a strong-named assembly.

它似乎确实像宣传的那样工作.如果不将程序集放在GAC中,则需要使用/ codebase以便可以访问文件路径.

上一篇:Photoshop打造彩色烟雾装饰的潮流字


下一篇:如何从C#查询VB6 IDE的模式