0
我目前正在實施asp.net mvc應用程序。我正在使用Autofac DI,並希望檢查是否具有與I相同的名稱前綴的類和接口。因此,爲了解決每個註冊問題,是否需要爲每個接口添加映射條目。目前項目中有50多個班。Autofac註冊類型與接口和類具有相同的名稱與前綴我的接口
感謝 聶
我目前正在實施asp.net mvc應用程序。我正在使用Autofac DI,並希望檢查是否具有與I相同的名稱前綴的類和接口。因此,爲了解決每個註冊問題,是否需要爲每個接口添加映射條目。目前項目中有50多個班。Autofac註冊類型與接口和類具有相同的名稱與前綴我的接口
感謝 聶
你可以使用反射來檢查根據您的會議名稱做一些的Starup。
我做過類似的,但檢查屬性的存在:
private static void RegisterServices()
{
var serviceList = AppDomain.CurrentDomain.GetAssemblies()
.Where(aa => aa.FullName.IndexOf("DI.Web.Ex09", StringComparison.Ordinal) > -1)
.SelectMany(a =>
a.GetTypes()
.Where(t => t.HasAttribute(typeof(InjectAttribute)) && t.IsPublic)
);
foreach (var t in serviceList)
{
var attribs = t.GetCustomAttributes<InjectAttribute>();
foreach (var attrib in attribs)
{
_builder.RegisterType(t)
.As(attrib.BindingType).InstancePerRequest();
}
}
}
查看完整的例子在這裏:
https://github.com/ovation22/IntroToDependencyInjection/tree/master/DI.Web.Ex09