2013-01-21 12 views
9

Sto provando a scrivere un metodo che trova tutti i tipi in un assembly con un attributo personalizzato specifico. Devo anche essere in grado di fornire un valore stringa da abbinare. L'avvertenza è che mi piacerebbe essere in grado di eseguire questo su qualsiasi classe e restituire qualsiasi valore.Richiama dinamicamente il valore dell'attributo di classe dal tipo

Per esempio: Vorrei eseguire una chiamata del genere

Type tTest = TypeFinder.GetTypesWithAttributeValue(Assembly.Load("MyAssembly"), typeof(DiagnosticTestAttribute), "TestName", "EmailTest"); 

Il mio metodo sembra così lontano come questo:

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 
} 

mio attributo è simile al seguente:

[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; 
    } 
} 

Per chi ha familiarità con le espressioni, mi piacerebbe davvero essere in grado di effettuare una chiamata li ke:

TypeFinder.GetTypesWithAttributeValue<DiagnosticTestAttribute>(Assembly.Load("MyAssembly"), x=> x.TestName, "EmailTest"); 

Dove l'espressione utilizza il mio tipo generico per selezionare la proprietà che sto cercando.

risposta

13

questo dovrebbe funzionare:

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 
} 

O ancora più bello:

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 
} 

Questo è come l'invocazione si presenta come:

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

e questo, rispettivamente:

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

Questo ha funzionato perfettamente. Ho dovuto cambiare l'assemblaggio come lo chiamavo da un altro ma funziona benissimo. Grazie mille. –

+0

se potessi darti più cred vorrei ;-) – Seabizkit

2

Molto tempo fa ho sviluppato alcuni metodi di supporto per estrarre un nome di proprietà da un'espressione,

Penso che sarebbe utile a voi.

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(); 
} 

Partenza this file di più.

+0

Ciò è utile. Appena riesco a capire come estrarre il valore da un attributo di una classe di istanza che dovrebbe aiutarmi a creare un'interfaccia piacevole per il mio metodo. Grazie! –

Problemi correlati