C# Inject

C# Inject

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace System
{
    partial class App
    {
        private class InjectItem
        {
            private ConcurrentDictionary<Guid, Assembly> _mapper;

            public AppDomain Domain { get; private set; }
            public ConcurrentDictionary<Guid, Assembly> Mapper
            {
                get { return _mapper; }
            }

            public InjectItem(AppDomain domain)
            {
                this.Domain = domain;
                _mapper = new ConcurrentDictionary<Guid, Assembly>();
            }
        }

        private static SynchronizedCollection<InjectItem> _injectSet = new SynchronizedCollection<InjectItem>();

        private static InjectItem CallInject(string domainName)
        {
            if (_injectSet == null)
            {
                var set = new SynchronizedCollection<InjectItem>();
                set.Add(new InjectItem(AppDomain.CurrentDomain));
                Interlocked.CompareExchange(ref _injectSet, set, null);
            }
            if (string.IsNullOrEmpty(domainName))
            {
                return _injectSet.First();
            }

            domainName = string.Format("_{0}", domainName);
            var q = from t in _injectSet
                    where t.Domain.FriendlyName == domainName
                    select t;
            var item = q.SingleOrDefault();
            if (item == null)
            {
                _injectSet.Add(item = new InjectItem(AppDomain.CreateDomain(domainName)));
            }
            return item;
        }

        public static Assembly Inject(Guid checksum, object arg, Stream rawStream = null, string domainName = null)
        {
            Contract.Requires(checksum != Guid.Empty);

            var item = CallInject(domainName);
            if (rawStream != null)
            {
                var raw = new MemoryStream();
                rawStream.FixedCopyTo(raw);
                raw.Position = 0L;
                Guid checksumNew = CryptoManaged.MD5Hash(raw);
                if (checksum != checksumNew)
                {
                    throw new InvalidOperationException("checksum");
                }
                return item.Mapper.GetOrAdd(checksum, k => item.Domain.Load(raw.ToArray()));
            }
            Assembly ass;
            if (!item.Mapper.TryGetValue(checksum, out ass))
            {
                throw new InvalidOperationException("checksum");
            }
            Type entryType = ass.GetType(string.Format("{0}.Program", ass.FullName), false);
            if (entryType == null)
            {
                var eType = typeof(IAppEntry);
                var q = from t in ass.GetTypes()
                        where t.IsSubclassOf(eType)
                        select t;
                entryType = q.FirstOrDefault();
            }
            if (entryType != null)
            {
                var creator = entryType.GetConstructor(Type.EmptyTypes);
                if (creator != null)
                {
                    var entry = (IAppEntry)creator.Invoke(null);
                    try
                    {
                        entry.Main(arg);
                    }
                    catch (Exception ex)
                    {
                        LogError(ex, "Inject");
#if DEBUG
                        throw;              
#endif
                    }
                }
            }
            return ass;
        }

        public static void Uninject(string domainName)
        {
            Contract.Requires(!string.IsNullOrEmpty(domainName));

            var item = CallInject(domainName);
            _injectSet.Remove(item);
            try
            {
                AppDomain.Unload(item.Domain);
            }
            catch (Exception ex)
            {
                LogError(ex, "Uninject");
#if DEBUG
                throw;
#endif
            }
        }
    }
}

 

posted on 2014-05-22 17:53 RockyLOMO 阅读(...) 评论(...) 编辑 收藏

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值