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
}
}
}
}
C# Inject的更多相关文章
- JAVA CDI 学习(1) - @Inject基本用法
CDI(Contexts and Dependency Injection 上下文依赖注入),是JAVA官方提供的依赖注入实现,可用于Dynamic Web Module中,先给3篇老外的文章,写得很 ...
- RCE via XStream object deserialization && SECURITY-247 / CVE-2016-0792 XML reconstruction Object Code Inject
catalogue . Java xStream . DynamicProxyConverter . java.beans.EventHandler . RCE via XStream object ...
- [译]View components and Inject in ASP.NET MVC 6
原文:http://www.asp.net/vnext/overview/aspnet-vnext/vc 介绍view components view components (VCs) 类似于part ...
- 安卓中級教程(2):@InjectView中的對象inject
package com.example.ele_me.util; import java.lang.annotation.Annotation; import java.lang.reflect.Fi ...
- 控制反转(IOC: Inverse Of Control) & 依赖注入(DI: Independence Inject)
举例:在每天的日常生活中,我们离不开水,电,气.在城市化之前,我们每家每户需要自己去搞定这些东西:自己挖水井取水,自己点煤油灯照明,自己上山砍柴做饭.而城市化之后,人们从这些琐事中解放了出来,城市中出 ...
- angular_$inject
<!DOCTYPE HTML> <html lang="zh-cn" ng-app="MainApp"> <head> &l ...
- javax.inject中@Inject、@Named、@Qualifier和@Provider用法
@Inject @Inject支持构造函数.方法和字段注解,也可能使用于静态实例成员.可注解成员可以是任意修饰符(private,package-private,protected,public).注 ...
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-001-使用Hibernate(@Inject、@EnableTransactionManagement、@Repository、PersistenceExceptionTranslationPostProcessor)
一.结构 二.Repository层 1. package spittr.db; import java.util.List; import spittr.domain.Spitter; /** * ...
- Spring Injection with @Resource, @Autowired and @Inject
August 1st, 2011 by David Kessler Overview I’ve been asked several times to explain the difference b ...
随机推荐
- stdlib标准库的常用API
iOS 有如下四种随机数方法,下面以产生 [0,100) 的随机数为例: 1. srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % ...
- PHP 实现多服务器共享 SESSION 数据
PHP 实现多服务器共享 SESSION 数据 2011 年 12 月 05 日评论暂缺 一.问题起源 稍大一些的网站,通常都会有好几个服务器,每个服务器运行着不同功能的模块,使用不同的二级域名,而一 ...
- json方法
http://penghuaiyi.iteye.com/blog/1922632 package com.yd.web.util; import java.lang.reflect.Type; imp ...
- Python开发者须知 —— Bottle框架常见的几个坑
Bottle是一个小巧实用的python框架,整个框架只有一个几十K的文件,但却包含了路径映射.模板.简单的数据库访问等web框架组件,而且语法简单,部署方便,很受python开发者的青睐.Pytho ...
- AngularJS的directive(指令)配置选项说明
js代码如下: var appModule = angular.module("appModule", []); appModule.controller("Ctrl&q ...
- 国内重要的MySQL传播平台
何登成 博客地址 http://hedengcheng.com/ 微博地址 @何_登成 评价:文笔细腻+理论知识丰富+实战Oracle+实战MySQL 吴炳锡 http://wubx.net/ hat ...
- C#的对象内存模型
转载自:http://www.cnblogs.com/alana/archive/2012/07/05/2577893.html C#的对象内存模型: 一.栈内存和堆内存1.栈内存 由编译器自动分配和 ...
- NodeJS 各websocket框架性能分析
For a current project at WhoScored, I needed to learn JavaScript, Node.js and WebSocket channel, aft ...
- js字符串函数之indexOf()
indexOf 返回字符串中指定字符首次出现的位置 var str="hello, I am Miss bean!"; str.indexOf("l")//结果 ...
- js一些题目
假期在家,看到的,昨天看了下: 原文链接:http://wwwcqamin.duapp.com/?p=102#comment-7 请说出下面程序的输出结果 第一题: 1 2 3 4 5 6 7 8 9 ...