首页 > 网站 > 建站经验 > 正文

asp调用c#编制-的com组件实例

2019-11-02 15:15:22
字体:
来源:转载
供稿:网友

 

1 新建类库MyTestDLL 

2 右击项目“MyTestDLL”-》属性-》生成-》勾选“为COM互操作注册” 

3 打开 AssemblyInfo.cs 文件 修改 [assembly: ComVisible(true)] 

4 打开Visual Sutdio 2008 的命令提示行工具输入guidgen.exe 选择DEFINE_GUID 单击 "New GUID" 

5代码 

      1、每个类名对应一个接口名,接口名是类名前加上一个大写的I 

      2、接口中声明的方法要使用属性 [DispId(n)] 

      3、类必须有一个无参构造函数  


Code 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 
namespace MyTestDll 


     //  这里Guid为第4步生成的。 
    [Guid("FFA4B191-FB5B-4dd5-B7B1-B2F32BF6F1FF")] 
    public interface IMyTestDll 
    { 
        [DispId(0)] 
        string GetAbout(); 
    } 
    public class Test1:IMyTestDll 
    { 
        PRivate string summary; 
        public Test1() 
        { 
            summary = "这是我的第一个测试"; 
        } 
        public string GetAbout() 
        { 
            return summary; 
        } 
    } 


  

6 生成项目 

asp测试代码   

<%    
  Dim  o     
  Set o = Server.CreateObject("MyTestDll.Test1") &nbs
搞笑段子[www.62-6.com]
p;  
  Response.Write o.GetAbout() 
  Set o=Nothing   
   
  %>    

提示:如果要在其他的电脑使用我们用C#开发的这个COM组件还需要是用regasm来注册 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表