C# Inject

时间:2021-09-22 12:39:22
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的更多相关文章

  1. JAVA CDI 学习&lpar;1&rpar; - &commat;Inject基本用法

    CDI(Contexts and Dependency Injection 上下文依赖注入),是JAVA官方提供的依赖注入实现,可用于Dynamic Web Module中,先给3篇老外的文章,写得很 ...

  2. RCE via XStream object deserialization &amp&semi;&amp&semi; SECURITY-247 &sol; CVE-2016-0792 XML reconstruction Object Code Inject

    catalogue . Java xStream . DynamicProxyConverter . java.beans.EventHandler . RCE via XStream object ...

  3. &lbrack;译&rsqb;View components and Inject in ASP&period;NET MVC 6

    原文:http://www.asp.net/vnext/overview/aspnet-vnext/vc 介绍view components view components (VCs) 类似于part ...

  4. 安卓中級教程(2):&commat;InjectView中的對象inject

    package com.example.ele_me.util; import java.lang.annotation.Annotation; import java.lang.reflect.Fi ...

  5. 控制反转&lpar;IOC&colon; Inverse Of Control&rpar; &amp&semi; 依赖注入(DI&colon; Independence Inject)

    举例:在每天的日常生活中,我们离不开水,电,气.在城市化之前,我们每家每户需要自己去搞定这些东西:自己挖水井取水,自己点煤油灯照明,自己上山砍柴做饭.而城市化之后,人们从这些琐事中解放了出来,城市中出 ...

  6. angular&lowbar;&dollar;inject

    <!DOCTYPE HTML> <html lang="zh-cn" ng-app="MainApp"> <head> &l ...

  7. javax&period;inject中&commat;Inject、&commat;Named、&commat;Qualifier和&commat;Provider用法

    @Inject @Inject支持构造函数.方法和字段注解,也可能使用于静态实例成员.可注解成员可以是任意修饰符(private,package-private,protected,public).注 ...

  8. SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-001-使用Hibernate&lpar;&commat;Inject、&commat;EnableTransactionManagement、&commat;Repository、PersistenceExceptionTranslationPostProcessor&rpar;

    一.结构 二.Repository层 1. package spittr.db; import java.util.List; import spittr.domain.Spitter; /** * ...

  9. Spring Injection with &commat;Resource&comma; &commat;Autowired and &commat;Inject

    August 1st, 2011 by David Kessler Overview I’ve been asked several times to explain the difference b ...

随机推荐

  1. 纯css来实现提示框

    用js用多了,就疏忽了最基本的css了---用title属性来实现提示框.下面言归正传------如何用css实现提示框: 1.利用title属性来实现鼠标滑过某个元素时,实现提示整段内容的功能(利用 ...

  2. Java应用程序监控JavaMelody

    JavaMelody是运行在Java Web容器中,用来监控Java内存和J服务器CPU使用情况,用户Session数量,JDBC连接数,和http请求.sql请求等的执行数量,平均执行时间,错误百分 ...

  3. 将枚举定义生成SQL中的Case-When-then语句

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. java源代码分析----jvm&period;dll装载过程

    简述众所周知java.exe是java class文件的执行程序,但实际上java.exe程序只是一个执行的外壳,它会装载jvm.dll(windows下,以下皆以windows平台为例,linux下 ...

  5. &lbrack;转载&rsqb;5分钟了解Mockito

    原文链接: http://liuzhijun.iteye.com/blog/1512780/ 5分钟了解Mockito 博客分类: Open SourceJava 一.什么是mock测试,什么是moc ...

  6. QT-Creator C&sol;C&plus;&plus; 打地鼠小游戏

    废话少说先上图: 这个游戏纯属土鳖思路,没有用到什么游戏引擎. 1.使用按钮或QLabel铺满窗口. 2.通过简单算法随机动态的设置按钮矩阵中某个按钮的背景图像. 3.同步2过程反复设置多个按钮背景实 ...

  7. 发布Ubuntu&sol;Linux系统cache,增加可用内存空间

    桌面Ubuntu总内存4G,但free只有内存有100M 重视top命令检查看到真正的能力free内存.以下是真正的内存使用情况的看法有一个命令. watch -n 1 cat /proc/memin ...

  8. kvm虚拟机管理 系统自动化安装

    原创博文安装配置KVM http://www.cnblogs.com/elvi/p/7718574.htmlweb管理kvm http://www.cnblogs.com/elvi/p/7718582 ...

  9. 将WTL应用向导添加到VS2019

    WTL 简介 WTL 全称是 Windows Template Library,像 MFC 一样使用 C++ 的面向对象技术对 Win32 接口进行了封装,使之便于开发 Windows 程序.相对于 ...

  10. es6学习笔记--模板字符串

    这几天简单看了一下深入浅出es6这本书,感觉特实用,学习了一个新特性---模板字符串在项目开发中,拼接字符串是不可缺少的,动态创建dom元素以及js操作数据都要拼接字符串,在es6出来之前,我们都通常 ...