使用C#创建ActiveX控件(译文)

首先创建DLL库,代码如下:
using System;
using System.Runtime.InteropServices;
namespace ANamespace
{
 // 定义COM组件的接口
  public interface ASignatures
  {
    string FName();
    string SName();
    int Age { get;} 
  }
 // 表明此类会被公开为一个COM组件的接口
  [ClassInterface(ClassInterfaceType.AutoDual)]
  public class AClass :ASignatures
  {
 // 具体实现接口的方法
    public string FName()
    {
      return "Imran";
    }
    public string SName()
    {
      return "Nathani";
    }
    public int Age
    {
      get { return 24; }
    }
  }
}
将上面的代码保存为AClass.cs,然后编译: csc /t:library AClass.cs
将得到一个AClass.dll,然后注册:regasm AClass.dll /tlb /codebase
最后创建一个HTML测试网页,内容如下:

<html>
<head>
  <script language="javascript">
    <!-- Load the ActiveX object  -->
    var x = new ActiveXObject("ANamespace.AClass");

    <!-- Access the Method -->
    alert(x.FName());
    alert(x.SName());

    <!-- Access the Property -->
    alert(x.Age);
  </script>
</head>
<body>
</body>
</html>

示例文件下载:/Files/margiex/testcom.rar
原文出处:http://dotnetslackers.com/articles/csharp/WritingAnActiveXControlInCSharp.aspx

原文地址:https://www.cnblogs.com/margiex/p/739336.html