我创建了自己的扩展,它根据我的接口 IBlock 解析类型。该扩展仅查看正在执行的程序集,但可能会查看您想要的任何位置。从这里应该可以做一个你想要的扩展。
块:
using Microsoft.Practices.Unity;
public interface IBlock
{
void Register(IUnityContainer unity);
}
延期:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Practices.Unity;
public class UnityBlockRegistrationExtender : UnityContainerExtension
{
private readonly NameToTypesMap nameToTypesMap = new NameToTypesMap();
protected override void Initialize()
{
var blockType = typeof(IBlock);
var blockTypes = Assembly.GetEntryAssembly().GetTypes()
.Where(block => blockType.IsAssignableFrom(block) && block.IsClass);
foreach (var type in blockTypes)
{
if (this.nameToTypesMap.AddType(type.AssemblyQualifiedName, type))
{
var block = this.Container.Resolve(type) as IBlock;
block.Register(this.Container);
}
}
}
private class NameToTypesMap
{
private readonly Dictionary<string, Type> map = new Dictionary<string, Type>();
internal bool AddType(string name, Type type)
{
if (name == null)
{
throw new ArgumentNullException("name", "A name is required.");
}
if (type == null)
{
throw new ArgumentNullException("type", "A Type object is required.");
}
lock (this.map)
{
if (!this.map.ContainsKey(name))
{
this.map[name] = type;
return true;
}
}
return false;
}
}
}