環境:.NET Framework 3.5 / Visual Studio 2008。
using System.Runtime.InteropServices;
[assembly: ComVisible(true)] // 類型對 COM 可見
[assembly: Guid("94882155-3B7C-48e3-B357-234D56D8F15E")] // 類型庫 ID
using System.Security;
[assembly: AllowPartiallyTrustedCallers()] // 允許部分信任呼叫者 (網頁場景需求)
注意:若類別未標註 Guid,COM 註冊會產生新 GUID,AssemblyInfo 的 Guid 僅作為類型庫 ID。
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace KeyActiveX
{
[Guid("94882155-3B7C-48e3-B357-234D56D8F15E")] // 成為 CLSID
public partial class IdentityKey : UserControl
{
}
}
此 Guid 將作為 HTML classid 調用依據。
<object id="controlbyid" classid="clsid:{94882155-3B7C-48e3-B357-234D56D8F15E}"></object>
在 IE 中啟用「不安全控制項」即可測試存取。
使用 OLE/COM Object Viewer (VC 安裝目錄) 在 .NET Categories 下查看組件與 CLSID。