.net注册机制案例

.net注册机制案例

----------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace JasonTest
{
   
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TestWorker testWorker = new TestWorker(args); // 实例化 TestWorker 类的实例
                testWorker.DoTestWork();
            }
            catch (LicenseException) // 捕获到 LicenseException 类型的异常,说明许可证验证失败
            {
                Console.WriteLine("License Deny !"); // 告诉你一声,你没这个权限 ^_^
            }
            Console.Write("Press ENTER for exit");
            Console.ReadLine();
        }
    }
    [Flags]
    public enum ArgType
    {
        None = 0,
    }
    class SimpleLicense : License
    {
        private Type _Type;
        public SimpleLicense(Type type)
        {
            if (type == null)
            {
                throw (new NullReferenceException());
            }
            _Type = type;
        }
        public override void Dispose()
        {
            // TODO: 根据需要插入垃圾回收的代码
        }
        public override string LicenseKey
        {
            get { return (_Type.GUID.ToString()); }
        }
    }
    public class SimpleLicenseProvider : LicenseProvider
    {
        public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
        {
            if (context.UsageMode == LicenseUsageMode.Runtime)
            {
                // 这里是验证运行时的许可证的逻辑
                // 这里我采用了随机数(因为这样写代码较少),也就是说有时候能用有时候禁用,你可以根据自己具体的需要编写不同的验证逻辑
                Random random = new Random();
                if (random.Next(0, 2) == 1)
                {
                    return (new SimpleLicense(type));
                }
                else
                {
                    throw (new LicenseException(type));
                }
            }
            else if (context.UsageMode == LicenseUsageMode.Designtime)
            {
                // TODO: 如果你想限制编辑模式下的许可证(所谓的开发许可证),在这里添加相应的逻辑
            }
            return (null);
        }
    }

    [LicenseProvider(typeof(SimpleLicenseProvider))] // 这里指定了这个类中进行许可证验证的过程应采用SimpleLicenseProvider
    public class TestWorker : IDisposable // 实现IDisposable接口,License的实例需要被显示的调用Dispose
    {
        private ArgType _ArgType = ArgType.None;
        License _License = null; // _License 用于保存许可证信息
        public TestWorker(string[] args)
        {
            ProcessArgs(args);
            _License = LicenseManager.Validate(typeof(TestWorker), this); // 在这里调用LicenseManager进行许可证验证
            // 如果验证失败Validate方法将会抛出 LicenseException 类型的异常
        }
        private void ProcessArgs(string[] args)
        {
            if (args != null)
            {
                if (args.Length > 0)
                {
                    foreach (string arg in args)
                    {
                        if (!string.IsNullOrEmpty(arg))
                        {
                            _ArgType |= ProcessArg(arg);
                        }
                    }
                }
                else
                {
                    _ArgType = ArgType.None;
                }
            }
            else
            {
                _ArgType = ArgType.None;
            }
        }
        protected virtual ArgType ProcessArg(string arg)
        {
            ArgType res = ArgType.None;
            // TODO:
            return (res);
        }
        public void DoTestWork()
        {
            OnDoTestWork();
        }
        protected virtual void OnDoTestWork()
        {
            // TODO:
        }
        public void Dispose()
        {
            if (_License != null)
            {
                _License.Dispose(); // 调用_License的Dispose方法,显示释放资源
            }
        }
    }
}

posted @ 2012-06-06 15:53  】Richard【  阅读(214)  评论(0编辑  收藏  举报