से प्राप्त करें मैं एक विधि लिखने की कोशिश कर रहा हूं जो एक विशिष्ट कस्टम विशेषता के साथ एक असेंबली में सभी प्रकार पाता है। मुझे मिलान करने के लिए एक स्ट्रिंग वैल्यू की आपूर्ति करने में भी सक्षम होना चाहिए। चेतावनी यह है कि मैं इसे किसी भी वर्ग पर चलाने और किसी भी मूल्य को वापस करने में सक्षम होना चाहता हूं।गतिशील रूप से वर्ग विशेषता मान
उदाहरण के लिए: मैं की तरह एक फोन पर अमल करना चाहते हैं इस
Type tTest = TypeFinder.GetTypesWithAttributeValue(Assembly.Load("MyAssembly"), typeof(DiagnosticTestAttribute), "TestName", "EmailTest");
मेरे विधि अब तक इस तरह दिखता है:
public static Type GetTypesWithAttributeValue(Assembly aAssembly, Type tAttribute, string sPropertyName, object oValue)
{
object oReturn = null;
foreach (Type type in aAssembly.GetTypes())
{
foreach (object oTemp in type.GetCustomAttributes(tAttribute, true))
{
//if the attribute we are looking for matches
//the value we are looking for, return the current type.
}
}
return typeof(string); //otherwise return a string type
}
मेरे गुण इस तरह दिखता है:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class DiagnosticTestAttribute : Attribute
{
private string _sTestName = string.Empty;
public string TestName
{
get { return _sTestName; }
}
public DiagnosticTest(string sTestName)
{
_sTestName = sTestName;
}
}
अभिव्यक्ति से परिचित लोगों के लिए, मैं वास्तव में कॉल ली बनाने में सक्षम होना चाहूंगा ke:
TypeFinder.GetTypesWithAttributeValue<DiagnosticTestAttribute>(Assembly.Load("MyAssembly"), x=> x.TestName, "EmailTest");
कहाँ अभिव्यक्ति संपत्ति है कि मैं के लिए देख रहा हूँ चयन करने के लिए मेरी सामान्य प्रकार का उपयोग करता।
यह बेकार ढंग से काम किया। मुझे असेंबली बदलनी पड़ी क्योंकि मैं इसे एक अलग से बुला रहा था लेकिन यह बहुत अच्छा काम करता है। बहुत बहुत धन्यवाद। –
अगर मैं आपको अधिक क्रेडिट दे सकता हूं तो मैं ;-) – Seabizkit