2017-09-18 36 views

回答

1

你可以使用反射來檢查根據您的會議名稱做一些的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

相關問題