这是一个简单的项目,只需几个步骤即可帮助您入门。
C#代码:
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly:System.CLSCompliant(true)]
[assembly: ComVisible(true)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7d9c5cd3-73d4-4ab1-ba98-32515256c0b0")]
namespace Cheeso.ComTests
{
[Guid("7d9c5cd3-73d4-4ab1-ba98-32515256c0b1")]
public class TestReply
{
public string salutation;
public string name;
public string time;
}
[Guid("7d9c5cd3-73d4-4ab1-ba98-32515256c0b2")]
public class TestObj
{
// ctor
public TestObj () {}
public TestReply SayHello(string addressee)
{
return SayHello(addressee, "hello");
}
public TestReply SayHello(string addressee, string greeting)
{
string x = String.Format("{0}, {1}!", greeting, addressee);
Console.WriteLine("{0}", x);
TestReply r = new TestReply
{
salutation = greeting,
name = addressee,
time = System.DateTime.Now.ToString("u")
};
return r;
}
}
}
VBScript 客户端代码:
Function Main()
Dim obj
Dim reply
set obj = CreateObject("Cheeso.ComTests.TestObj")
Set reply = obj.SayHello("Evgeny")
WScript.Echo "Reply at: " & reply.time
Set reply = obj.SayHello_2("Evgeny", "wassup")
WScript.Echo "Reply at: " & reply.time
End Function
Main
构建:
(produce your .snk file, once)
csc.exe /t:library /debug+ /keyfile:Foo.snk /out:TestObj.dll TestObj.cs
regasm /codebase TestObj.exe
然后只需运行 vbscript(通过 cscript.exe)。
一旦你让基本的东西工作起来,你可以调整它,添加 GAC,使 typelib 显式,添加显式 ProgId,等等。
ps:仅供参考,此示例显示了在为互操作注册的类上重载 .NET 方法会发生什么。方法名称后附加了一个隐式 _2(_3、_4 等)。