Je commence à travailler avec des attributs et pour cet exemple le code est écrit en utilisant PostSharp 2.0.Est-il possible d'extraire ou de sauvegarder des données d'un attribut?
Ce que je voudrais faire est la méthode qui a l'attribut Stopwatch
ou la méthode d'appel peut accéder au temps écoulé.
Je vous écris actuellement ce code en C# 4.0
code d'attribut Exemple:
[Serializable]
public class StopwatchAttribute : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
// Create new stopwatch
Stopwatch stopwatch = new Stopwatch();
// Begin timing
stopwatch.Start();
args.MethodExecutionTag = stopwatch;
}
public override void OnExit(MethodExecutionArgs args)
{
Stopwatch stopwatch = (Stopwatch)args.MethodExecutionTag;
// Stop timing
stopwatch.Stop();
// Write result
Debug.WriteLine("Time elapsed: {0} ms", stopwatch.Elapsed);
}
}
En utilisant l'attribut
[Stopwatch]
public string Search(string LineNo)
{
// Search for something
// Is it possible to know how long (from Stopwatch attribute) Search took
}
public void CallSearch
{
string x = Search("xyz");
// Is it possible to know how long (from Stopwatch attribute) Search took
}
Je reçois une sortie de temps écoulé.
Cependant, est-il possible de prolonger ceci afin que je puisse envoyer le temps écoulé.
Idéalement je voudrais voir combien de temps Search
a pris de CallSearch
, mais ce sera ok si je peux même obtenir le résultat dans Search
lui-même, puis revenir en quelque sorte elle.
Pour contourner le problème, j'utilise actuellement le modèle "Hole in the Middle".
Merci
je l'avais fait chose similaire en utilisant [Unité Interception] (http://pnpguidance.net/Post/UnityInterceptionExtensionExampleTransparentProxyPolicyInjectorHandlerAttributeICallHandler.aspx) –
@Sanjeevakumar Je suis désolé, je ne pouvais pas comprendre comment le lien fournies par votre fait ce que je cherche. Pourriez-vous s'il vous plaît fournir plus de détails. –