初めて実際に手動でアセンブリをスキャンする必要がありました。私は、メソッドレベルにカスタム属性のすべてのクラスとメソッドをスキャンするためのベストプラクティス
var methodsWithAttributes =
(from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
from method in type.GetMethods()
let attributes = method.GetCustomAttributes(typeof(SomeAttribute), true)
where attributes != null && attributes.Length > 0
select new { Type = type, Method = method,
Attributes = attributes.Cast<SomeAttribute>() })
.ToList();
を拡大するのに十分な単純なものでした
var typesWithMyAttribute =
(from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
let attributes = type.GetCustomAttributes(typeof(SomeAttribute), true)
where attributes != null && attributes.Length > 0
select new { Type = type, Attributes = attributes.Cast<SomeAttribute>() })
.ToList();
で私を設定C# - how enumerate all classes with custom class attribute?に出くわした私は、単一のスキャンでこれを行うためにこれらの2を結合しようとする必要があり、または初期の最適化に落としただけですか? (スキャンはアプリケーションの起動時にのみ実行されます)
アセンブリの型よりはるかに多くのメソッドがあるので、メソッドのスキャンのために最適な方法がありますか?
私はアセンブリおよび種類を列挙すると、アセンブリと型をキャッシュすることであるように、発見し、あなたの属性をインスタンス化するよりもはるかに高速になるだろうことを賭けるつもりです無意味になるだろう。 – Gabe