C# 7 Local Functions: are attributes / aspects allowed?

我怕爱的太早我们不能终老 提交于 2019-12-21 04:53:14

问题


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?


回答1:


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.

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



来源:https://stackoverflow.com/questions/43498834/c-sharp-7-local-functions-are-attributes-aspects-allowed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!