Syntax for applying an attribute to an anonymous method?

This is due to a question about return type attributes and anonymous classes , but then for anonymous methods (or lambdas), but so far I could find this exact question, it seems, until it is in stackoverflow.

In the code for business objects that we generate using CodeSmith, we now have attributes [DebuggerNonUserCode], so they are not taken into account in the results of code coverage. Unfortunately, the generated code uses anonymous methods, which are now still displayed in code scope with type names Class.<>c__DisplayClass3cdue to how they are actually handled by the compiler.

A quick code example, with names and types changed to protect the innocent, so to speak:

    public delegate T ReturnSomething<T>();

    public static T SafeCall<T>(T whenNotSupported, ReturnSomething<T> method)
    {
        T result;
        try
        {
            result = method();
        }
        catch (NotSupportedException)
        {
            result = whenNotSupported;
        }
        return result;
    }

    public static void CodeExample()
    {
        string foo = SafeCall<string>("OOPS!", delegate
        {
            //throw new NotSupportedException();
            return "Ok";
        });
    }

[DebuggerNonUserCode] , , , ? , ?

[DebuggerNonUserCode] method SafeCall ( ReturnSomething<T>) , , , , . :

    public static void CodeExample()
    {
        string foo = SafeCall<string>("OOPS!", [DebuggerNonUserCode] delegate
        {
            //throw new NotSupportedException();
            return "Ok";
        });
    }

CSharp, , ( lambdas). , (?) ...?

+5
3

, . 401 # 3.0:

scope ( ) (§9.6), - (§10.1.5), -- (§13.2), struct-member-declarations (§11.2), enum-member-declarations (§14.3), - (§10.7.2), event-accessor-declarations (§10.8.1), (§10.6.1).

+9

, # .

, DebuggerNonUserCode ? - , , , .

+1

Since your goal is to exclude methods from the Cover Code, you can do this by creating a .runsettings file instead of adding an attribute to the method:

http://msdn.microsoft.com/en-us/library/jj159530.aspx

+1
source

All Articles