使用C#属性来跟踪函数调用,变量和返回值? [英] Use C# attribute to track function call, variables, and return value?

查看:489
本文介绍了使用C#属性来跟踪函数调用,变量和返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我可以使用装饰跟踪函数调用,它的变量和返回值。
这是非常容易使用。
我只是想知道能C#做同样的事情?

我发现有CallTracing样本code在线属性。
但是,它并没有显示结果我的预期。

C#的属性是否也有类似的概念Python的装饰?

谢谢您
和祝商祺!

  [AttributeUsage(AttributeTargets.Method | AttributeTargets.ReturnValue |
    AttributeTargets.Property,的AllowMultiple = FALSE)]
公共类CallTracingAttribute:属性
{
    公共CallTracingAttribute()
    {        尝试
        {
            堆栈跟踪堆栈跟踪=新的堆栈跟踪();
            的StackFrame的StackFrame = stackTrace.GetFrame(1);            Trace.TraceInformation({0} - > {1} {2}:{3},
                stackFrame.GetMethod()。ReflectedType.Name,
                stackFrame.GetMethod()名,
                stackFrame.GetFileName(),
                stackFrame.GetFileLineNumber());            的Debug.WriteLine(的String.Format({0} - > {1} {2}:{3},
                stackFrame.GetMethod()。ReflectedType.Name,
                stackFrame.GetMethod()名,
                stackFrame.GetFileName(),
                stackFrame.GetFileLineNumber()));
        }
        抓住
        {
        }
    }
}类节目
{
    [CallTracing]
    静态INT测试(INT一)
    {
        返回0;
    }    [CallTracing]
    静态无效的主要(字串[] args)
    {
        试验(1);
    }
}


解决方案

.NET不支持呼叫拦截架构,如果你愿意的一些限制(即自ContextBoundObject派生,除其他事项外)居住。你可以找到如何做到这一点在 MSDN杂志artcile名为解耦通过注射定制服务到您的对象的拦截链。

组件

另外,你可能要考虑抽象出你的类到接口,然后通过你的接口实现转发他们截获的电话。

最后,来看看WCF。它有一个非常明确的拦截架构(不要让事实,这是Web服务欺骗你,你可以简单地创建自​​己的信道传输这是一个空信道),您可以利用来做你想做的。

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

解决方案

.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#属性来跟踪函数调用,变量和返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆