IComponentContext
から特定のインタフェースを実装する登録済みType
のリストを取得する必要があります。Autofacのインタフェースの登録済み実装をすべて取得
タイプの実際のインスタンスを望んでいませんが、のインスタンスを取得するType
のリストがあります。
このリストを使用して、メッセージバス上のサブスクリプションを生成します。
登録されたインターフェイスの実装をすべてAutofacで取得するにはどうすればよいですか?
IComponentContext
から特定のインタフェースを実装する登録済みType
のリストを取得する必要があります。Autofacのインタフェースの登録済み実装をすべて取得
タイプの実際のインスタンスを望んでいませんが、のインスタンスを取得するType
のリストがあります。
このリストを使用して、メッセージバス上のサブスクリプションを生成します。
登録されたインターフェイスの実装をすべてAutofacで取得するにはどうすればよいですか?
私はこれを考え出した -
var types = scope.ComponentRegistry.Registrations
.SelectMany(r => r.Services.OfType<IServiceWithType>(), (r, s) => new { r, s })
.Where(rs => rs.s.ServiceType.Implements<T>())
.Select(rs => rs.r.Activator.LimitType);
ServiceTypeにImplementsメソッドがありません! –
それでも最新のAutofacで動作します。 –
をでAutoFac 3.5.2(この記事に基づいて:http://bendetat.com/autofac-get-registration-types.html)
が最初にこの機能を実装します。
using Autofac;
using Autofac.Core;
using Autofac.Core.Activators.Reflection;
...
private static IEnumerable<Type> GetImplementingTypes<T>(ILifetimeScope scope)
{
//base on http://bendetat.com/autofac-get-registration-types.html article
return scope.ComponentRegistry
.RegistrationsFor(new TypedService(typeof(T)))
.Select(x => x.Activator)
.OfType<ReflectionActivator>()
.Select(x => x.LimitType);
}
その後、仮定あなたは持っていますbuilder
、
var container = builder.Build();
using (var scope = container.BeginLifetimeScope())
{
var types = GetImplementingTypes<T>(scope);
}
Reflectionを使用して、アセンブリ内のすべての型を反復処理し、それらが 'IComponentContext'を実装しているかどうか確認しましたか? [C#3.5でインターフェイスを実装するすべての型を取得する](http://stackoverflow.com/questions/26733/getting-all-types-that-implement-an-interface-with-c-sharp-3-5)を参照してください。 –
@NikolayKhilそれは問題ではありません。私はコンテキストを見て、登録されたタイプを見つける必要があります。これはAutofac固有の質問です。 –