1 Create a new class library MyTestDLL
2 Right-click the project "MyTestDLL"-"Properties-"Generate-"Check "Register for COM interop"
3 Open the AssemblyInfo.cs file and modify [assembly: ComVisible(true)]
4 Open the command prompt tool of Visual Sutdio 2008, enter guidgen.exe, select DEFINE_GUID and click "New GUID"
5 codes
1. Each class name corresponds to an interface name. The interface name is the class name plus a capital I.
2. The method declared in the interface must use the attribute [DispId(n)]
3. The class must have a parameterless constructor
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace MyTestDll
{
//The Guid here is generated in step 4.
[Guid("FFA4B191-FB5B-4dd5-B7B1-B2F32BF6F1FF")]
public interface IMyTestDll
{
[DispId(0)]
string GetAbout();
}
public class Test1:IMyTestDll
{
PRivate string summary;
publicTest1()
{
summary = "This is my first test";
}
public string GetAbout()
{
return summary;
}
}
}
6 Generate project
asp test code
<%
Dim o
Set o = Server.CreateObject("MyTestDll.Test1")
Response.Write o.GetAbout()
Set o=Nothing
%>
Tip: If you want to use the COM component we developed in C# on other computers, you also need to register it with regasm.
The method is:
First, copy the files in the binDebug directory to the target computer, then open the command prompt tool and enter:
regasm The directory/filename.dll you copied to /tlb f:/dll/filename.tlb /codebase
It can be run on this computer.
References:
http://topic.csdn.net/u/20080625/13/0294fe91-200c-4939-b36b-c9a2c6781354.html
http://topic.csdn.net/t/20060314/15/4613620.html
http://cplus.e800.com.cn/articles/2009/211/1234338268521_3.html
http://topic.csdn.net/t/20020712/10/868557.html
http://www.itzhe.cn/news/20071123/21768.html
http://www.cnblogs.com/illele/archive/2007/10/25/937050.html
-