Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用类似于python的c语言编写函数装饰器_C#_.net 4.5 - Fatal编程技术网

C# 如何用类似于python的c语言编写函数装饰器

C# 如何用类似于python的c语言编写函数装饰器,c#,.net-4.5,C#,.net 4.5,我在python上工作了一段时间,回到c#进行一个项目。所以我使用python语言,这迫使我像python程序员一样思考,我喜欢这样 我想问的问题是,如何创建一个在其decarator之后调用的方法 Python装饰器语法: def p_decorate(func): def func_wrapper(name): return "<p>{0}</p>".format(func(name)) return func_wrapper @p_dec

我在python上工作了一段时间,回到c#进行一个项目。所以我使用python语言,这迫使我像python程序员一样思考,我喜欢这样

我想问的问题是,如何创建一个在其decarator之后调用的方法

Python装饰器语法:

def p_decorate(func):
   def func_wrapper(name):
       return "<p>{0}</p>".format(func(name))
   return func_wrapper

@p_decorate
def get_text(name):
   return "lorem ipsum, {0} dolor sit amet".format(name)

来自面向方面编程中经常使用的示例代码,两个常用的库是和

下面是一个PostSharp示例,它是原始python示例的一部分

using System;
using System.Reflection;
using PostSharp.Aspects;
using PostSharp.Extensibility;

namespace SandboxConsole
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine(GetText("Test"));
            Console.ReadLine();
        }

        [Decorate]
        public static string GetText(string name)
        {
            return String.Format("lorem ipsum, {0} dolor sit amet", name);
        }
    }

    [Serializable]
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class DecorateAttribute : MethodInterceptionAspect
    {
        public override bool CompileTimeValidate(MethodBase method)
        {
            if (!((MethodInfo)method).ReturnType.IsAssignableFrom(typeof(string)))
            {
                Message.Write(SeverityType.Error, "CUSTOM01", "Can not apply [Decorate] to method {0} because it does not retun a type that is assignable from string.", method);
                return false;
            }
            return true;
        }

        public override void OnInvoke(MethodInterceptionArgs args)
        {
            args.Proceed();
            args.ReturnValue = String.Format("<p>{0}</p>", args.ReturnValue);
        }
    }
}
使用系统;
运用系统反思;
使用PostSharp.Aspects;
可扩展性;
名称空间沙盒控制台
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(GetText(“Test”);
Console.ReadLine();
}
[装饰]
公共静态字符串GetText(字符串名称)
{
返回String.Format(“lorem ipsum,{0}dolor sit amet”,name);
}
}
[可序列化]
[AttributeUsage(AttributeTargets.Method,AllowMultiple=true)]
公共类装饰属性:MethodInterceptionSpect
{
公共重写bool CompileTimeValidate(MethodBase方法)
{
if(!((MethodInfo)method).ReturnType.IsAssignableFrom(typeof(string)))
{
Message.Write(SeverityType.Error,“CUSTOM01”,“无法对方法{0}应用[decoration],因为它不会重新运行可从字符串“”中赋值的类型,方法);
返回false;
}
返回true;
}
公共覆盖无效OnInvoke(MethodInterceptionArgs args)
{
args.procedure();
args.ReturnValue=String.Format(“{0}

”,args.ReturnValue); } } }
您可以使用nuget软件包:

我只需写下面的代码来解释我想做什么:我认为您需要更明确地说明您需要做什么。我不能从你的示例代码中分辨出来。更重要的是,示例.NET代码和Python代码之间没有对称性,这使得它更加混乱。也许只有我一个人。对不起,我走了,请提供更多信息是不是
p_decoration
,当应用于任何方法时,会拦截对该方法的任何调用,执行自己的代码,并在选择时导致执行它所修饰的方法?如果是这样的话,在C#中没有完全类似的功能可以直接使用,但是在面向方面的编程中也可以使用类似的功能。有一些框架,比如PostSharp,可以让您实现这一点。您所描述的示例c#(客户MVC授权属性)代码不是用ASP.NET中的装饰器完成的-有许多关于如何实现这一点的在线资源。
using System;
using System.Reflection;
using PostSharp.Aspects;
using PostSharp.Extensibility;

namespace SandboxConsole
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine(GetText("Test"));
            Console.ReadLine();
        }

        [Decorate]
        public static string GetText(string name)
        {
            return String.Format("lorem ipsum, {0} dolor sit amet", name);
        }
    }

    [Serializable]
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class DecorateAttribute : MethodInterceptionAspect
    {
        public override bool CompileTimeValidate(MethodBase method)
        {
            if (!((MethodInfo)method).ReturnType.IsAssignableFrom(typeof(string)))
            {
                Message.Write(SeverityType.Error, "CUSTOM01", "Can not apply [Decorate] to method {0} because it does not retun a type that is assignable from string.", method);
                return false;
            }
            return true;
        }

        public override void OnInvoke(MethodInterceptionArgs args)
        {
            args.Proceed();
            args.ReturnValue = String.Format("<p>{0}</p>", args.ReturnValue);
        }
    }
}