使用C#属性来跟踪函数调用、变量和返回值?
在Python中,我可以使用装饰器来跟踪函数调用、它的变量和返回值。 它非常容易使用。 我只是想知道 C# 可以做同样的事情吗?
我发现网上有CallTracing Attribute的示例代码。 然而,它并没有显示出我预期的结果。
C# 属性与 python 的装饰器有类似的概念吗?
感谢您 并致以最诚挚的问候!
[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);
}
}
In Python, I can use decorators to trace the function call, its variables and return values.
It is very easy to use.
I just wonder can C# do the same thing?
I find out there is a sample code of CallTracing Attribute online.
However, it didn't show the result I expected.
Does C# attributes have similar concepts as python's decorator?
Thanks you
and Best Regards!
[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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您愿意接受某些约束(即从 ContextBoundObject 派生等),.NET 确实支持调用拦截体系结构。您可以在 标题为“解耦”的 MSDN 杂志文章中找到有关如何执行此操作的完整说明通过将自定义服务注入到对象的拦截链中来构建组件”。
另外,您可能需要考虑将类抽象为接口,然后通过接口实现转发调用来拦截调用。
最后看一下WCF。它有一个非常明确的拦截架构(不要让它的 Web 服务欺骗了您,您可以简单地创建自己的通道传输,这是一个空通道),您可以利用它来完全执行您想要的操作。
.NET does support a call-interception architecture, if you are willing to live by some constraints (namely, deriving from ContextBoundObject, among other things). You can find a full description of how to do this in the MSDN magazine artcile titled "Decouple Components by Injecting Custom Services into Your Object's Interception Chain".
Also, you might want to consider abstracting out your classes into interfaces and then intercepting the calls by forwarding them through your interface implementations.
Finally, take a look at WCF. It has a very well-defined interception architecture (don't let the fact that it's web services fool you, you can simply create your own channel transport which is a null channel) which you can leverage to do exactly what you want.
C# 不支持开箱即用的 AOP。为此,您需要一个像 PostSharp 这样的 IL 重写器。
C# doesn't support AOP out of the box. You need an IL rewriter like PostSharp for that.
您可以使用 运行时流程来跟踪所有 .NET 函数调用、参数和返回值,而无需手动添加装饰器 工具(由我开发)。
You can track all .NET function calls, parameters and return values without manually adding decorators using the Runtime Flow tool (developed by me).