vb 调用c#做的com 组件

vb 调用c#做的com 组件 1:建立c# 项目 (注意项目必须含有接口,其他类应该实现该接口,才可以在其他语言中用该com组件) 2:将 AssemblyInfo.cs 里的 [assembly: ComVisible(false)] 改为 true 3:项目-属性-生成里将:为com interop 注册 选种 4:生成后就可以在其他语言中引用或该组件。 一般代码如下: using System; using System.Collections.Generic; using System.Text; namespace UseMethod { public interface IField { void SetField(UseMethod.Fields f); UseMethod.Fields GetField(); UseMethod.Fields GetSetField(UseMethod.Fields f); int Add(int i, int j); } public class FieldAction:IField { public FieldAction() { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } public void SetField(UseMethod.Fields f) { Fields f1 = new Fields(); f1.city = "city"; f1.country = "country"; f1.myCity = "nycity"; f1.myName = "myName"; } public UseMethod.Fields GetField() { Fields f1 = new Fields(); f1.city = "city"; f1.country = "country"; f1.myCity = "nycity"; f1.myName = "myName"; return f1; } public UseMethod.Fields GetSetField(UseMethod.Fields f) { return f; } public int Add(int i, int j) { return i + j; } } public class Fields { public string city; public string country; public string myName; public string myCity; } } 当然可以有其他复杂的类,但是在操作时,最好用强类型,以便于识别! 将生成的UseMethod.tlb 即.tlb文件引用到vb的项目中,然后 在vb6中调用: Private Sub cmdObject_Click() Dim meth As UseMethod.IField Dim met As UseMethod.FieldAction Dim result As UseMethod.Fields Dim fi As UseMethod.Fields Set fi = New UseMethod.Fields Set met = New UseMethod.FieldAction With fi .city = "city" .country = "country" .myName = "myName" .myCity = "MYcITR" End With Set meth = met txtStr.Text = meth.Add(8, 9) Set result = meth.GetSetField(fi) txtInOut.Text = result.city & result.country //注意这里的属性应和c#里定义的一样 MsgBox "soapClient3.AddressName", vbDefaultButton1, "提示" End Sub
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值