我無法將C#代碼翻譯成VB 6(在looooong中沒有這樣做)。有沒有一種解決方案,我可以在.NET中編寫代碼,然後讓VB6調用該函數?有沒有辦法讓VB 6應用程序調用.NET程序集?
1
A
回答
5
是的,您可以使用regasm.exe實用程序將您的託管程序集作爲COM對象公開。爲了使這些類可見,組件或各個類需要使用[ComVisible(true)]
屬性進行標記。一旦該組件暴露爲COM對象,就可以像使用任何標準COM對象一樣從VB6中使用它。
3
暴露你的.NET方法COM,你需要創建一個接口:
[Guid("CF4CDE18-8EBD-4e6a-94B4-6D5BC0D7F5DE")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
public interface IFoo {
[DispId(1)]
string MyMethod(string value);
}
你的類會從接口派生:
[Guid("7EBD9126-334C-4893-B832-706E7F92B525")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[ProgId("MyNamespace.Foo")]
public class Foo: IFoo {
public string MyMethod(string value){
return somestring;
}
}
從VB6,您的通話將看起來像這樣:
Dim oFoo as New Foo
dim sReturn as string
sReturn = oFoo.MyMethod("someValue")