日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

NET许可证及License

發(fā)布時(shí)間:2025/3/17 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。