C#7局部函数:允许使用属性/方面吗? [英] C# 7 Local Functions: are attributes / aspects allowed?

查看:143
本文介绍了C#7局部函数:允许使用属性/方面吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#7引入了局部功能(太好了!).假设我有以下代码:

C# 7 introduced local functions (which is great!). Suppose I have the following code:

    using System;
    using PostSharp.Aspects;

    namespace AspectCS7
    {
        class Program
        {
            private static void Main()
            {
                [MyAspect]            
                void LocalFunction()
                {
                    Console.WriteLine("Hello Aspect!");
                }

                LocalFunction();
            }
        }

        [Serializable]
        public class MyAspect : OnMethodBoundaryAspect
        {
            public override void OnEntry(MethodExecutionArgs args)
            {
                Console.WriteLine("Entering Aspect");
            }
        }
    }

此代码显示编译时错误.可以将属性应用于局部函数吗?

This code shows compile-time errors. Is it possible to apply attributes to local functions?

推荐答案

在某一点允许在本地函数上使用属性.在本地有一些网络上的示例使用属性的函数,但是不再允许使用它们.

Attributes were allowed on local functions at one point. There are some examples on the web of local functions using attributes, however they're not allowed anymore.

更新: 这是有关此主题的正在进行的讨论: https://github.com/dotnet/csharplang/issues/794 .

Update: Here is an ongoing discussion on this topic: https://github.com/dotnet/csharplang/issues/794.

这篇关于C#7局部函数:允许使用属性/方面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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