以下のカスタム属性があります。Castle Windsor の慣習による登録に依存するのではなく、明示的な登録が必要なインターフェイスにラベルを付けることです。
using System;
[AttributeUsage(
AttributeTargets.Interface | AttributeTargets.Class,
Inherited = true)]
public class ExplicitDependencyRegistrationRequiredAttribute : Attribute
{
}
これは、次のようにインターフェイスに適用されます。
using Dependencies.Attributes;
[ExplicitDependencyRegistrationRequired]
public interface IRandomNumberGenerator
{
int GetRandomNumber(int max);
}
以下に簡単な具体的な実装を示します。
using System;
public class RandomNumberGenerator : IRandomNumberGenerator
{
private readonly Random random = new Random();
public int GetRandomNumber(int max)
{
return random.Next(max);
}
}
これは、Castle Windsor で規則に従ってコンポーネントを登録するときに、登録の重複や例外の追加について心配する必要がないという考えです。代わりに、インターフェイスがこの属性でマークされていることを確認する必要があります。次に、それをフィルタリングするコードを以下に示します。
Type exclusionType = typeof(ExplicitDependencyRegistrationRequiredAttribute);
BasedOnDescriptor selectedTypes =
Classes
.FromAssembly(assembly)
.Where(t => !Attribute.IsDefined(t, exclusionType, true))
.WithServiceAllInterfaces();
問題は、Attribute.IsDefined
フィルターが機能していないように見えることです。存在する属性を持つインターフェイスから継承するコンポーネントがまだ登録されています。
クラスに属性を明示的に追加するとRandomNumberGenerator
、フィルターが機能します。ただし、インターフェイスから継承していないように見えるか、 Castle Windsor がカスタム属性を正しく取得していません。
どんな助けでもいただければ幸いです