ठीक है, आप सभी विधानसभाओं कि वर्तमान एप्लिकेशन डोमेन में लोड कर रहे हैं में सभी कक्षाओं के माध्यम से गणना करने के लिए होगा। ऐसा करने के लिए, आप GetAssemblies
method को वर्तमान ऐप डोमेन के लिए AppDomain
उदाहरण पर कॉल करेंगे।
वहां से, आप GetExportedTypes
(यदि आप केवल सार्वजनिक प्रकार चाहते हैं) या GetTypes
पर प्रत्येक Assembly
पर असेंबली में मौजूद प्रकार प्राप्त करने के लिए कॉल करेंगे।
फिर, आप प्रत्येक Type
उदाहरण पर GetCustomAttributes
method पर कॉल करेंगे, जिस विशेषता को आप ढूंढना चाहते हैं।
आप आप के लिए यह आसान बनाने के लिए 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>() };
ऊपर क्वेरी अपनी विशेषता लागू किया गया, विशेषता (विशेषताओं) यह करने के लिए आवंटित के कहने के साथ साथ आप प्रत्येक प्रकार मिल जाएगा।
नोट अगर आप अपने आवेदन डोमेन में लोड विधानसभाओं की एक बड़ी संख्या है, कि आपरेशन महंगा हो सकता है। तुम इतनी तरह, आपरेशन के समय को कम करने के Parallel 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>() };
एमएसडीएन उदाहरण लिंक एक मृत लिंक है। – MadTigger