¿Usar el atributo C # para rastrear llamadas a funciones, variables y valor de retorno?
En Python, puedo usar decoradores para rastrear la llamada a la función, sus variables y valores de retorno. Es muy fácil de usar. Me pregunto si C # puede hacer lo mismo.
Descubrí que hay un código de muestra de CallTracing Attribute en línea. Sin embargo, no mostró el resultado que esperaba.
¿Los atributos de C # tienen conceptos similares al decorador de Python?
Gracias y Saludos cordiales!
[AttributeUsage(AttributeTargets.Method | AttributeTargets.ReturnValue |
AttributeTargets.Property, AllowMultiple = false)]
public class CallTracingAttribute : Attribute
{
public CallTracingAttribute()
{
try
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
Trace.TraceInformation("{0}->{1} {2}:{3}",
stackFrame.GetMethod().ReflectedType.Name,
stackFrame.GetMethod().Name,
stackFrame.GetFileName(),
stackFrame.GetFileLineNumber());
Debug.WriteLine(string.Format("{0}->{1} {2}:{3}",
stackFrame.GetMethod().ReflectedType.Name,
stackFrame.GetMethod().Name,
stackFrame.GetFileName(),
stackFrame.GetFileLineNumber()));
}
catch
{
}
}
}
class Program
{
[CallTracing]
static int Test(int a)
{
return 0;
}
[CallTracing]
static void Main(string[] args)
{
Test(1);
}
}