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

18 comments:

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

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 :)

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.


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.

A less-trivial query arises when such a technique is referred to as from a constructor or Uk Thesis static constructor. Have to the compiler simply pass the call of the elegance, considering that’s what the member is named in supply code? If so, there could be no manner to differentiate between an example constructor and a static constructor.

Our company helps you to partner closely with engineering teams to develop and latest truly troublesome application using AWS – the globe’s most wide-ranging adopted cloud platform. cloud application modernization services

A very good article. I really got a lot of information in this article. This information about Subtleties of C 5’s is really good. Now it's time to avail HALFCASTE CREAM SET for more information.

More grateful to your wonderful blog...keep sharing us...cómo solicitar el divorcio nueva jersey

The async and await keywords, which made asynchronous programming easier, were assignment writing services added in C# 5. When a method is marked as async and await is used to watch for the end of asynchronous actions

ive into the subtleties of C#5's new [CallerMemberName] feature – a game-changer for code flexibility and readability! Unlock the intricacies and enhance your programming skills. Stressed about exams? Seek live exam help to navigate the complexities and ace your C's.

Great insights into the intricacies of [CallerMemberName] attribute behavior! Your thorough exploration of corner cases and thoughtful considerations, especially in scenarios like property accessors and constructor calls, demonstrates a deep understanding. Your blog provides valuable clarity on potential challenges developers may face, showcasing your expertise. Well done on shedding light on such nuanced aspects, making it an insightful read for anyone working with these attributes in Visual Studio 11 beta. abogado delitos sexuales nueva jersey | Ayuda Violencia Doméstica Nueva Jersey

super slot ค่ายเกมสล็อตออนไลน์ที่เว็บ PGSLOT ของเรานำมาให้ลูกค้าทุกท่านได้เล่น เพื่อความสนุกสนานเพลิดเพลิน ที่มาพร้อมกับความปลอดภัย และรวดเร็วของแอดมินตลอดทั้งวัน

Post a Comment