2009-03-24 4 views

Répondre

1

Vous pouvez obtenir la fonction de l'appelant à partir de la trace de la pile, et d'interroger ses attributs:

System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(); 
Object[] attr = 
    st.GetFrame(1).GetMethod().GetCustomAttributes(typeof(RunOnPlatformBAttribute), false); 
if (attr.Length > 0) { 
    // Yes, it does have the attribute RunOnPlatformB 
} 
0

d'abord, vous devez aller le StackFrame pour trouver ce que vous a appelé, dans mon expérience, c'est une opération horriblement coûteuse, et peut avoir des problèmes de sécurité aussi bien selon le contexte que vous utilisez. Le code sera quelque chose comme ceci -

using System.Diagnostics; 
using System.Reflection; 

.... 
    StackTrace stackTrace = new StackTrace();   
    StackFrame[] stackFrames = stackTrace.GetFrames(); 

    StackFrame caller = stackFrames[1]; 

    MethodInfo methodInfo = caller.GetMethod() as MethodInfo; 
    foreach (Attribute attr in methodInfo.GetCustomAttributes()) 
    .....