2013-01-21 14 views
9

से प्राप्त करें मैं एक विधि लिखने की कोशिश कर रहा हूं जो एक विशिष्ट कस्टम विशेषता के साथ एक असेंबली में सभी प्रकार पाता है। मुझे मिलान करने के लिए एक स्ट्रिंग वैल्यू की आपूर्ति करने में भी सक्षम होना चाहिए। चेतावनी यह है कि मैं इसे किसी भी वर्ग पर चलाने और किसी भी मूल्य को वापस करने में सक्षम होना चाहता हूं।गतिशील रूप से वर्ग विशेषता मान

उदाहरण के लिए: मैं की तरह एक फोन पर अमल करना चाहते हैं इस

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"); 

कहाँ अभिव्यक्ति संपत्ति है कि मैं के लिए देख रहा हूँ चयन करने के लिए मेरी सामान्य प्रकार का उपयोग करता।

उत्तर

13

यह काम करना चाहिए:

public static Type GetTypeWithAttributeValue<TAttribute>(Assembly aAssembly, Func<TAttribute, object> pred, object oValue) { 
    foreach (Type type in aAssembly.GetTypes()) { 
    foreach (TAttribute oTemp in type.GetCustomAttributes(typeof(TAttribute), true)) { 
     if (Equals(pred(oTemp), oValue)) { 
     return type; 
     } 
    } 
    } 
    return typeof(string); //otherwise return a string type 
} 

या यहां तक ​​कि अच्छे:

public static Type GetTypeWithAttributeValue<TAttribute>(Assembly aAssembly, Predicate<TAttribute> pred) { 
    foreach (Type type in aAssembly.GetTypes()) { 
    foreach (TAttribute oTemp in type.GetCustomAttributes(typeof(TAttribute), true)) { 
     if (pred(oTemp)) { 
     return type; 
     } 
    } 
    } 
    return typeof(string); //otherwise return a string type 
} 

यह कैसे मंगलाचरण लग रहा है जैसे:

GetTypeWithAttributeValue<DefaultValueAttribute>(Assembly.GetCallingAssembly(), attribute => attribute.Value, 
                "string"); 

और क्रमश: इस एक:

GetTypeWithAttributeValue<DefaultValueAttribute>(Assembly.GetCallingAssembly(), 
                attribute => attribute.Value == "string"); 
+0

यह बेकार ढंग से काम किया। मुझे असेंबली बदलनी पड़ी क्योंकि मैं इसे एक अलग से बुला रहा था लेकिन यह बहुत अच्छा काम करता है। बहुत बहुत धन्यवाद। –

+0

अगर मैं आपको अधिक क्रेडिट दे सकता हूं तो मैं ;-) – Seabizkit

2

बहुत वर्ष पहले मैं,

एक अभिव्यक्ति से एक प्रॉपर्टी नाम निकालने के लिए कुछ सहायक तरीकों का विकास किया गया है मुझे लगता है कि यह आप के लिए उपयोगी होगा।

public static string Item<TItem, TMember>(this TItem obj, Expression<Func<TItem, TMember>> expression) 
{ 
    if (expression.Body is MemberExpression) 
    { 
     return ((MemberExpression)(expression.Body)).Member.Name; 
    } 
    if (expression.Body is UnaryExpression) 
    { 
     return ((MemberExpression)((UnaryExpression)(expression.Body)).Operand).Member.Name; 
    } 
    if (expression.Body is ParameterExpression) 
    { 
     return expression.Body.Type.Name; 
    } 
    throw new InvalidOperationException(); 
} 

अधिक के लिए this file देखें।

+0

यह सहायक है। जैसे ही मैं यह समझ सकता हूं कि एक आवृत्ति वर्ग की विशेषता से मूल्य निकालने का तरीका है जो मुझे मेरी विधि के लिए एक अच्छा इंटरफ़ेस बनाने में मदद कर सकता है। धन्यवाद! –

संबंधित मुद्दे