c#编写com组件以及在silverlight中调用

1.创建windows类库项目

2.AssemblyInfo.cs设置ComVisible(false) --> ComVisible(true)

3.项目属性中,Build选项中勾选

4.往下,Signing选项中注册程序集,勾选Sign the assembly, 选择New...

5.输入名字,随便自己取个名字,去掉Project my key file a password

6.修改成自己的类名

7.编写代码

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

namespace RXCOM
{
[ComVisible(true)]
[Guid("39116335-A4BB-4A2B-A461-90779B386CE5")]
public interface IRXCOM

{
void Initialize();
void Dispose();
int Add(int x, int y);
}
[ComVisible(true)]
[Guid("C944C444-6231-4515-9422-48B4B7277462")]
[ProgId("RXCOM.IMCOM")]
public classIMCOM :IRXCOM
{
public void Initialize()
{
//nothing todo
}
public void Dispose()
{
//nothing todo
}
public int Add(int x, int y)
{
return x + y;
}
}

}

8、GUID属性里面的那个字符串,在“Tools”菜单下面,“Create GUID”
选择 Registry Format,Copy,Exit

9.编译dll,使用命令C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe RXCOM.dll注册com。

以上com组件已经编写完成。下面介绍如何在silverlight中调用该组件

-----------------------------------------------------------------------------------------------------------------------------

1.silverlight项目中必须选择OOB模式,并且修改安全级别,勾选Enable running application out of the browser

2.打开Out-of-Browser Settings,勾选如下

3.编写silverlight调用代码,执行接口中的方法

dynamic IMDCOM = AutomationFactory.CreateObject("RXCOM.IMCOM");

dynamic result = IMDCOM.Add(100,200);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值