Syntax for applying an attribute to an anonymous method?
Asked Answered
C

4

6

This is related to the question about return type attributes and anonymous classes, but then for anonymous methods (or lambdas), but as far I could find this exact question does not seem to be on stackoverflow yet.

In code for business entities that we generate using CodeSmith we now have [DebuggerNonUserCode] attributes, so they don't count in code coverage results. Unfortunately, the generated code uses anonymous methods that now still show up in code coverage with names like Class.<>c__DisplayClass3c because of the way these are actually handled by the compiler.

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

Is there a way to get [DebuggerNonUserCode] attributes on these methods so we could get rid of the name-mangled anonymous method names from our generated code from our code coverage results? Or do we need to rewrite that generated code to no longer use anonymous methods?

Putting the [DebuggerNonUserCode] on the method parameter of the SafeCall method definition (before the ReturnSomething<T> parameter type) does not compile and maybe would not do exactly what we would like if it would. The following also does not compile:

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

I've tried to have a quick look at the CSharp Language Specification, but have not had any luck finding a syntax that would allow applying attributes to anonymous methods (or lambdas). Did I miss it, or is this (currently?) impossible...?

Cromorne answered 15/9, 2009 at 12:19 Comment(1)
Don't think this will get high priority (if it'll ever get done), but I have added the suggestion to MS Connect at connect.microsoft.com/VisualStudio/feedback/…Cromorne
H
9

You cannot, unfortunately. It is listed on page 401 of the C#3.0 language specification:

Attributes can be specified at global scope (to specify attributes on the containing assembly or module) and for type-declarations (§9.6), class-member-declarations (§10.1.5), interface-member-declarations (§13.2), struct-member-declarations (§11.2), enum-member-declarations (§14.3), accessor-declarations (§10.7.2), event-accessor-declarations (§10.8.1), and formal-parameter-lists (§10.6.1).

Holman answered 15/9, 2009 at 12:26 Comment(1)
Thanks, that's the reference I was looking for. :-(Cromorne
L
1

Unfortunately C# does not allow attributes to be applied to anonymous methods.

Are you sure that you really want to apply a DebuggerNonUserCode attribute to this anonymous method? The method is user code and I would think that you would want to be able to step into it if need be.

Legible answered 15/9, 2009 at 12:24 Comment(1)
Like I said, the SafeCall method is also called in CodeSmith generated code. There we would like to put a [DebuggerNonUserCode] on the anonymous method, yes.Cromorne
R
1

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

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

Ruisdael answered 11/3, 2014 at 4:8 Comment(0)
A
0

Starting with C# 10, the custom attributes can be applied to anonymous methods, like so:

var choose = [DebuggerNonUserCode] object (bool b) => b ? 1 : "two";

More information: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions#attributes

Ausgleich answered 22/9, 2022 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.