我們將介紹C# COM組件的ASP調(diào)用方法,首先需要建立類庫,然后是在Visual Sutdio 2008 的命令提示行進(jìn)行操作。本文能對大家了解C# COM組件有所幫助。
1 新建類庫MyTestDLL
2 右擊項(xiàng)目“MyTestDLL”-》屬性-》生成-》勾選“為COM互操作注冊”
3 打開 AssemblyInfo.cs 文件 修改 [assembly: ComVisible(true)]
4 打開Visual Sutdio 2008 的命令提示行工具輸入guidgen.exe 選擇DEFINE_GUID 單擊 "New GUID"
5代碼
1、每個(gè)類名對應(yīng)一個(gè)接口名,接口名是類名前加上一個(gè)大寫的I
2、接口中聲明的方法要使用屬性 [DispId(n)]
3、類必須有一個(gè)無參構(gòu)造函數(shù)
- 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 = "這是我的第一個(gè)測試";
- }
- public string GetAbout()
- {
- return summary;
- }
- }
- }
6 生成項(xiàng)目
ASP測試代碼
- <%
- Dim o
- Set o = Server.CreateObject("MyTestDll.Test1")
- Response.Write o.GetAbout()
- Set o=Nothing
- %>
提示:如果要在其他的電腦使用我們這個(gè)C# COM組件還需要是用regasm來注冊
方法為:
首先把bin\Debug目錄的文件拷貝到目標(biāo)電腦上,然后打開命令提示行工具輸入:
regasm 你拷貝到的目錄/文件名.dll /tlb f:/dll/文件名.tlb /codebase
運(yùn)行既可在該電腦上使用。
原文標(biāo)題:使用ASP調(diào)用C#寫的COM組件
聯(lián)系客服