Subtleties of C# 5’s new [CallerMemberName]

UPDATE: Now that the Visual Studio 11 beta has shipped with this feature implemented, I wrote a separate blog post exploring how it actually behaves in these corner cases.

Last time, I explored various pathological code samples in which the [CallerLineNumber] attribute does not have obvious behavior.  This time, I’ll cover the last of these new caller info attributes: [CallerMemberName].

The [CallerMemberName] attribute tells the compiler to insert the name of the containing member instead of a parameter’s default value.  Unlike [CallerLineNumber] and [CallerFileName], this has no equivalent in C++; since the C / C++ versions of these features are in the preprocessor, they cannot be aware of member names.

Most calls to methods with optional parameters take place within a named method, so the behavior of this attribute is usually obvious.  However, there are a couple of places where the exact method name is not so obvious.

If you call a [CallerMemberName] method inside a property or event accessor, what name should the compiler pass?  Common sense indicates that it should pass the name of the property or event, since that’s the name you actually see in source code.  That would also allow this attribute to be used for raising PropertyChanged events.  However, this option doesn’t pass enough information, since it would not be possible to determine whether it was called from the getter or the setter.  To expose the maximal amount of information, the compiler should pass the name of the actual method for the accessor – get_Name or set_Name

I would assume that the compiler only passes the property name, since that is what most people would probably expect.

A less-trivial question arises when such a method is called from a constructor or static constructor.  Should the compiler just pass the name of the class, since that’s what the member is named in source code? If so, there would be no way to distinguish between an instance constructor and a static constructor.  Should the compiler pass the actual names of the CLR methods (.ctor and .cctor)?  If so, there would be no way to tell the class name, which is worse.  Should it pass both (ClassName.ctor)? That would expose the maximal amount of information, but wouldn’t match the behavior in other members, which does not include the class name.

On a related note, what about calls to base or this constructors that take [CallerMemberName] arguments? Is that considered part of the class’ constructor, even though the call is lexically scoped outside the constructor?  If not, what should it pass?

A further related concern is field initializers. Since field initializers aren’t explicitly in any member, what should the compiler pass if you call a [CallerMemberName] method in a field initializer? I would assume that they’re treated like contructors (or static constructors for static field initializers)

I would assume that a call to a [CallerMemberName] method from within an anonymous method or LINQ query would use the name of the parent method.

The most interesting question concerns attributes.  What should happen if you declare your own custom attribute that takes a [CallerMemberName] parameter, then apply the attribute somewhere without specifying the parameter?

If you place the attribute on a parameter, return value, or method, it would make sense for the compiler to pass the name of the method that the attribute was applied to.  If you apply the attribute to a type, it might make sense to pass the name of that type.  However, there is no obvious choice for attributes applied to a module or assembly.

I suspect that they instead chose to not pass these caller info in default parameters for attribute declarations, and to instead pass the parameters’ declared default values.  If so, it would make sense to disallow caller info attributes in attribute constructor parameters.  However, this would also prevent them from being used for attributes that are explicitly instantiated in normal code (eg, for global filters in MVC).

Next Time: Caller Info Attributes vs. Stack Walking

22 comments:


The article you have shared here very good. This is really interesting information for me. Thanks for sharing!
hotmail.com login |hotmail log out |gmail login

We have to See if we can do to fulfill our knowledge and people always asking how i can get information fast
happy wheels game||happy wheels||fireboyandwatergirl.co||http://happywheels.in

Excellent work! Keep it up.
look page

Your blog is great. I read a lot of interesting things from it. Thank you very much for sharing. Hope you will update more news in the future.
games for kids online

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

I visit your web page. It is really useful and easy to understand. Hope everyone get benefit. Thanks for sharing your Knowledge and experience with us.
Norton setup - Get started with Norton by downloading the setup and installing it on the device. Enter the unique 25-character alphanumeric product key for activation. Check your subscription norton.com/setup | norton.com/setup | norton.com/setup |norton.com/setup

We are available 24/7 to help you out the complete procedure of download and install of www.office.com/setup. And also Check out here complete steps for www.mcafee.com/activate for downloading, installing, uninstalling, and activating, and also get antivirus help for www.norton.com/setup. For norton activation, get the 25 characters alphanumeric product key from Norton offers hordes of reliable antivirus programs.

You are doing a great job. Now we are sharing with you all the Tresurebox store products which you will be buy online in a single click. Buy Cheap Furniture nz online in a single click,

Norton is one of the top most antivirus for computers and gadgets, to download and setup Norton antivirus you need to visit Norton.com/setup or www.norton.com/setup. In this article we will guide you all steps which are necessary to activate Norton antivirus on windows.

This is an amazing blog, thank you so much for sharing such an valuable information with us.
Brother printer support |Dell Printer Support| Lexmark Printer Support | HP Printer Support

If you face any trouble at www.norton.com/setup in the course of redeeming Product key, down load and installing norton setup, Please call us toll-free USA/CANADA: +1-855-619-5888
Australia: +61-800-941-031
UK: +44-800-041-8972

Have you had any difficulties writing an essay? Don't worry, on this site https://payforessay.net/ you will write different essays on different topics: mathematics, physics, economic problems. You can also write everything from your resume to your thesis.


Good post thanks for share information. Lee Priest


Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.

Adding Exponents
fiu canvas
interval notation
Area of a Hexagon


Your style and passion for blogging is contagious. Thank you for sharing this way!
m4ufree

Even if you choose to go with a cosmetic tattoo artist without a medical degree, talk to your dermatologist to make sure your skin is amenable to a tattoo. Bray turns away Permanent Makeup NYC potential clients with oily skin or large pores. "Microblading only looks great when you can achieve fine details," says Bray. "People with oily skin won't heal with the crisp strokes you need to achieve a natural look."

I am not a keen reader but your blog was so well written that I couldn't close the tab. I really liked the collection and writting style. The blog really helped me solve my financial worries without being too overpowering. Really well written

Improve Credit Score
Improve Credit Score
personal funding atlanta
credit repair in atlanta

it would bode well for the compiler to pass the name of the technique. Custom Homework Service that the characteristic was applied to. On the off chance that you apply the characteristic to a kind, it may bode well to pass the name of that sort.

Great Article Cyber Security Projects projects for cse Networking Security Projects JavaScript Training in Chennai JavaScript Training in Chennai The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

Post a Comment