NET许可证及License
生活随笔
收集整理的這篇文章主要介紹了
NET许可证及License
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
有時(shí),我們需要為類或組件等添加許可。
而NET FCL為???我們提供了一些相關(guān)類的使用。
這些類都在System.ComponentModel命名空間下。
下面是簡(jiǎn)單的一個(gè)實(shí)現(xiàn):
?
using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?Microsoft.Win32;
using?System.Runtime.InteropServices;
using?System.ComponentModel;
/**//*
?*?Test?by?McJeremy&Xu
?*?url:http://www.cnblogs.com/mcjeremy
?*?
?*/
namespace?MyLicenseTest
{
????/**//*
?????*?License許可由License(許可證)、LicenseProvider(許可證提供器)、LicenseManager管理三部分組成
?????*?使用過(guò)程一般如下:
?????*?1、創(chuàng)建繼承自License并實(shí)現(xiàn)其抽象屬性LicenseKey的License類
?????*?2、創(chuàng)建繼承自LicenseProvider并實(shí)現(xiàn)其抽象方法GetLicense的Provider類
?????*?3、在需要驗(yàn)證許可的類或組件(等)的構(gòu)造方法中使用LicenseMangager的靜態(tài)Validate或IsValid方法,其中
?????*?Validate產(chǎn)生異常,而IsValid不產(chǎn)生
?????*/
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????try
????????????{
????????????????MyLicenseTestClass?mltc?=?new?MyLicenseTestClass();
????????????????Console.WriteLine("MyLicenseTestClass被許可使用了!");
????????????}
????????????catch?(LicenseException?e)
????????????{
????????????????Console.WriteLine(e.Message);
????????????}
????????????Console.ReadLine();
????????}
????}
????//Step1:實(shí)現(xiàn)License類
????public?class?SimpleRuntimeLicense?:?License
????{
????????private?string?TypeCode;
????????public?override?string?LicenseKey
????????{
????????????get?{?return?TypeCode;?}
????????}
????????public?override?void?Dispose()
????????{
????????}
????????public?SimpleRuntimeLicense(Type?type)
????????{
????????????this.TypeCode?=?type.GUID.ToString();
????????}
????}
????//Step2:實(shí)現(xiàn)licenseProvider類,使用Provider設(shè)計(jì)模式
????//NET提供了LicFileLicenseProvider類,一般情況下,我們需要提供我們自己的Provider來(lái)實(shí)現(xiàn)Validate邏輯
????public?class?SimpleRuntimeLicenseProvider?:?LicenseProvider
????{
????????/**////?<summary>
????????///?
????????///?</summary>
????????///?<param?name="context">驗(yàn)證上下文????</param>
????????///?<param?name="type">被驗(yàn)證對(duì)象類型</param>
????????///?<param?name="instance">被驗(yàn)證對(duì)象實(shí)例</param>
????????///?<param?name="allowExceptions">是否在驗(yàn)證沒(méi)通過(guò)時(shí)拋出異常</param>
????????///?<returns>驗(yàn)證通過(guò)后的許可證</returns>
????????public?override?License?GetLicense(LicenseContext?context,?Type?type,?object?instance,?bool?allowExceptions)
????????{
????????????//通過(guò)licenseUsageMode可以指定驗(yàn)證范圍是在設(shè)計(jì)時(shí)還是運(yùn)行時(shí)
????????????if?(context.UsageMode?==?LicenseUsageMode.Runtime?||?context.UsageMode?==?LicenseUsageMode.Runtime)
????????????{
????????????????RegistryKey?rk?=?Registry.LocalMachine.OpenSubKey(@"SOFTWARE\NETLicenseTest");
????????????????if?(null?!=?rk)
????????????????{
????????????????????try
????????????????????{
????????????????????????string?lick?=?rk.GetValue("SN").ToString();
????????????????????????if?(!string.IsNullOrEmpty(lick))
????????????????????????{
????????????????????????????if?(string.Compare(lick,?type.GUID.ToString(),true)?==?0)
????????????????????????????{
????????????????????????????????return?new?SimpleRuntimeLicense(type);
????????????????????????????}
????????????????????????}
????????????????????}
????????????????????catch?{
????????????????????????//設(shè)定沒(méi)有許可證時(shí)的提供信息
????????????????????????throw?new?LicenseException(type,?instance,?"您沒(méi)有許可證!無(wú)法運(yùn)行!");
????????????????????}
????????????????}
????????????}
????????????return?null;
????????}
????}
????//Step3:在需要許可驗(yàn)證的類型中使用LicenseProvider和LicenseManager????
????[Guid("7F46DB6D-98CD-4cb7-BA95-014F678B2375")]
????[LicenseProvider(typeof(SimpleRuntimeLicenseProvider))]???
????public?class?MyLicenseTestClass:IDisposable
????{
???????
????????License?lic?=?null;
????????public?MyLicenseTestClass()
????????{
????????????lic=LicenseManager.Validate(this.GetType(),?this);????????????
????????????//LicenseManager.IsValid(
????????}
????????IDisposable?成員#region?IDisposable?成員
????????private?bool?disposed?=?false;
????????void?IDisposable.Dispose()
????????{
????????????Dispose(true);
????????????GC.SuppressFinalize(this);
????????}
????????public?void?Dispose(bool?disposing)
????????{
????????????if?(!disposed)
????????????{
????????????????if?(disposing)
????????????????{
????????????????????if?(null?!=?lic)
????????????????????{
????????????????????????lic.Dispose();
????????????????????}
????????????????}
????????????}
????????}
????????~MyLicenseTestClass()
????????{
????????????Dispose(false);
????????}
????????#endregion
????}
}
轉(zhuǎn)載于:https://www.cnblogs.com/McJeremy/archive/2009/04/10/1432913.html
總結(jié)
以上是生活随笔為你收集整理的NET许可证及License的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: maven处理和java平级的资源文件
- 下一篇: JEECG-V3 版本相关文档开放通知