カスタム属性で属性付けされた 1 つ以上の型がアセンブリに含まれておりMyAttribute
、これらの型のリストを取得する必要がある場合を考えてみましょう。よりコンパクトな構文以外に、IsDefinedとGetCustomAttributesを使用する利点はありますか? ある人は、他の人がしない何かを公開/非表示にしますか? 一方は他方よりも効率的ですか?
以下は、それぞれの使用法を示すコード サンプルです。
Assembly assembly = ...
var typesWithMyAttributeFromIsDefined =
from type in assembly.GetTypes()
where type.IsDefined(typeof(MyAttribute), false)
select type;
var typesWithMyAttributeFromGetCustomAttributes =
from type in assembly.GetTypes()
let attributes = type.GetCustomAttributes(typeof(MyAttribute), false)
where attributes != null && attributes.Length > 0
select type;