ठीक है, आपको उन सभी असेंबली में सभी वर्गों के माध्यम से गणना करना होगा जो वर्तमान ऐप डोमेन में लोड किए गए हैं। ऐसा करने के लिए, आप मौजूदा ऐप डोमेन के लिए GetAssembliesविधि पर कॉल करेंगे AppDomain।
वहां से, आप विधानसभा में निहित प्रकारों को प्राप्त करने के लिए GetExportedTypes(यदि आप केवल सार्वजनिक प्रकार चाहते हैं) या GetTypesप्रत्येक पर कॉल करेंगे Assembly।
फिर, आप प्रत्येक उदाहरण पर GetCustomAttributesएक्सटेंशन विधि को कॉल करेंगे Type, जिस प्रकार की विशेषता को आप ढूंढना चाहते हैं।
आप इसे सरल बनाने के लिए LINQ का उपयोग कर सकते हैं:
var typesWithMyAttribute =
from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetTypes()
let attributes = t.GetCustomAttributes(typeof(HelpAttribute), true)
where attributes != null && attributes.Length > 0
select new { Type = t, Attributes = attributes.Cast<HelpAttribute>() };
उपरोक्त क्वेरी आपको उस पर लागू अपनी विशेषता के साथ प्रत्येक प्रकार के साथ मिल जाएगी, साथ ही उसे सौंपे गए विशेषता (एस) के उदाहरण के साथ।
ध्यान दें कि यदि आपके पास बड़ी संख्या में असेंबली आपके एप्लिकेशन डोमेन में लोड हैं, तो यह ऑपरेशन महंगा हो सकता है। आप ऑपरेशन के समय को कम करने के लिए समानांतर LINQ का उपयोग कर सकते हैं , जैसे:
var typesWithMyAttribute =
// Note the AsParallel here, this will parallelize everything after.
from a in AppDomain.CurrentDomain.GetAssemblies().AsParallel()
from t in a.GetTypes()
let attributes = t.GetCustomAttributes(typeof(HelpAttribute), true)
where attributes != null && attributes.Length > 0
select new { Type = t, Attributes = attributes.Cast<HelpAttribute>() };
विशिष्ट पर इसे छानना Assemblyसरल है:
Assembly assembly = ...;
var typesWithMyAttribute =
from t in assembly.GetTypes()
let attributes = t.GetCustomAttributes(typeof(HelpAttribute), true)
where attributes != null && attributes.Length > 0
select new { Type = t, Attributes = attributes.Cast<HelpAttribute>() };
और अगर असेंबली में बड़ी संख्या में प्रकार हैं, तो आप फिर से समानांतर LINQ का उपयोग कर सकते हैं:
Assembly assembly = ...;
var typesWithMyAttribute =
// Partition on the type list initially.
from t in assembly.GetTypes().AsParallel()
let attributes = t.GetCustomAttributes(typeof(HelpAttribute), true)
where attributes != null && attributes.Length > 0
select new { Type = t, Attributes = attributes.Cast<HelpAttribute>() };