Exploring Caller Info Attributes

Last year, Microsoft announced a simple new feature in C# 5: Caller Info Attributes.  These attributes let you to create methods with optional parameters and tell the compiler to pass the caller’s filepath, line number, or member name instead of the parameter’s default value.  This allows you to create logging methods that automatically know where they’re being called.

When the feature was announced, I wrote a couple of blog posts that delved into some of the corner cases of the new feature.  At the time, there was no public implementation, so they were pure conjecture.

This morning, Microsoft released the beta of Visual Studio 11, which is the first public build supporting these attributes.  Now, I can finally test my theories.  Here are the results:

Although these classes are new to the .Net Framework 4.5, you can still use this feature against older framework versions by creating your own classes in the System.Runtime.CompilerServices namespace.  However, the feature will only work if the code calling the method is compiled with the C# 5 compiler; older compilers will ignore the attributes and simply pass the parameters’ default values.

All of the attributes can only be applied to arguments of types that have standard (not custom) implicit conversions to int or string.  This means that it isn’t practical to overflow [CallerLineNumber] (the compiler ran out of memory first), so I can’t test how that behaves.

Using [CallerMemberName] on field initializers passes the field name, and on static or instances constructors passes the string ".cctor" or ".ctor" (as documented)  In indexers, it passes "Item".

If a class has a constructor that takes only caller info attribute parameters, and you create another class that inherits it and does not declare a constructor (thus implicitly passing optional parameters), it passes the line number and file name of the class keyword in the derived class, but leaves the declared default for the member name (I suspect that’s a bug). 

If you do declare a constructor, it passes the string ".ctor" as the member name for the implicit base() call (just like a normal method call from inside a constructor) and the line number of the beginning of the constructor declaration.  If you actually write a base() call, it passes the line number of the base keyword.

If a call spans multiple lines, [CallerLineNumber] passes the line containing the openning parenthesis.

Delegates are fully supported; if you call a delegate that has an argumented annotated with a caller info attribute, the compiler will insert the correct value, regardless of the method you’re actually calling (which the compiler doesn’t even know).

LINQ query comprehension syntax is not supported at all; if you create a (for example) Select() method that contains a caller info attribute, then call it from a LINQ query (not lambda syntax), the compiler will crash (!).  (they will fix that)

Expression trees do not support optional parameters at all, so that corner case is irrelevant.

Attributes are the most interesting story.  What should happen if you declare a custom attribute that takes parameters with caller info attributes, then apply that attribute in various cases?  This could potentially be very useful, since there is currently no way for an attribute to know what it’s being applied to. (I hadn’t thought of this usage when I wrote the original blog post)

The documentation says that this will work in all cases, and that [CallerMemberName] will pass whatever the attribute is being applied to.  However, in the beta build, this doesn’t always work.

Attributes applied to method arguments or return values do not pass any caller info at all.  Attributes applied to types or generic type arguments do not pass member names (this is very disappointing)

Hopefully, those will be fixed before release.

131 comments:

This comment has been removed by the author.

This blog article is nice and I like it, thank you :)

This blog article is nice, thank you for informastion...

Thanks for sharing such informative guide on .Net technology. This post gives me detailed information about the .net technology. I am working as trainer in leading IT training academy offering Dot Net Training in Chennai and i use your guide to educate my students. .Net Training in Chennai

This blog article is very good, I like it !!!

Thanks really valuable. Will certainly share site with my good friends.
hotmail login

Wow, wonderful blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is excellent, as well as the content!.
88sears

I enjoyed over read your blog post. Your blog have nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again.
* Street view

kami dari Murni Jaya Printing menyediakan Cetak Spanduk Murah di Jakarta Timur jadi hubungi kami jika anda butuh Cetak Spanduk 24 Jam di Jakarta Timur

nice and I like it, thank you
Wait for next article

Your site has a lot of useful information for myself. I visit regularly. Hope to have more quality items
stickman

Jasa Jilid Hard Cover Murah 24 Jam Jakarta Pusat Salemba
Jasa Jilid Hard Cover Murah 24 Jam Jakarta Pusat
Jasa Jilid Murah Jakarta Pusat, Salemba
Jasa Jilid Soft Cover Murah 24 jam Jakarta Pusat Salemba
Jasa Jilid Soft Cover Murah 24 jam Jakarta Pusat

Thanks for sharing informative article. Download Windows 7 ultimate for free from getintopc. It helps you to explore full functionality of windows operating system.

Your blog have nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again. gmail login

we from printing want to distribute printing services, photocopy, binding, cheap print, our goal to simplify your more difficulty with the services written here

click below
Percetakan Kilat
Percetakan Jakarta
Cetak Buku Yasin
Percetakan Rawamangun

Awesome post for bloggers. I am always believe most of the good information from trusted people like you.

Hadoop training in chennai

TEMPAT PRINT MURAH ONLINE 24 JAM DI JAKARTA TIMUR

TEMPAT PRINT MURAH ONLINE 24 JAM DI JAKARTA TIMUR

CETAK YASIN ONLINE MURAH JAKARTA TIMUR

I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
Best Hadoop Training Institute In chennai

A list of NRL Matches being televised in the UK via Watch NRL App and Website. watchnrl.com.
state of origin portal
state of origin online

I would like to read more of your posts. Very nice post thank you for sharing.
temple run 3

Nice Blog, Thanks for sharing this valuable one.This is very useful for me and gain more information,

Dot Net Training in Chennai | Dot Net Course in Chennai

Thanks for sharing information with clear explanation. This is really awesome to understand.

Thanks,
QTP Training in Chennai | QTP Course in Chennai

Awesome post for bloggers. I am always believe most of the good information from trusted people like you.
http://www.metaforumtechnologies.com/seo-training-in-chennai

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
Click here:
Angularjs training in chennai

Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
Click here:
Microsoft Azure training in online

hello sir,
thanks for giving that type of information.
digital marketing company in delhi

nice work keep it up thanks for sharing the knowledge.Thanks for sharing this type of information, it is so useful.
Laminated Doors manufacturer in hubli

nice topic which you have choose.
second is, the information which you have provided is better then other blog.
so nice work keep it up. And thanks for sharing.
outdoor led wall lights in delhi

I would like to say thank you for the amazing details and concepts you are sharing in this.

Oracle PLSQL Training in Chennai
Oracle PLSQL Training

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

Really cool post, highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you for this rare info!
industrial course in chennai

A small number of my blog audience have complained about my site not working correctly in Explorer but looks great in Safari. Do you have any ideas to help fix this problem?
iosh course in chennai

THANKS FOR INFORMATION

you can search low-cost website with high-quality website functions.
Today Join Us
Call: +91 - 8076909847

website designing company in Delhi

levantro
interior designer in delhi


livewebindia
website development company in delhi

SEO company in Delhi

Best It Service Provider:

1. Website Designing And Development.
2. SEO Services.
3. Software Development.
4. Mobile App Development.

Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..please sharing like this information......
PHP interview questions and answers | PHP interview questions | PHP interview questions for freshers | PHP interview questions and answers for freshers | php interview questions and answers for experienced | php viva questions and answers | php based interview questions

Nice article Thanks for sharing the informative blog.

redbeardpress
Education

This comment has been removed by the author.
This comment has been removed by the author.

I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.
fire and safety course in chennai

This comment has been removed by the author.

With 2 Way SMS, you can get customers to answer quick surveys and polls via text messaging. It may be the least bothersome way to get them to answer as compared to chasing them in malls.

Play and earn from online casinos at BGAOC we have new online casino games The best online resource for making money.

or those of you who are looking for name tage printing services in Jakarta
http://www.offsetprinting21.com/2019/01/jasa-cetak-name-tag-di-jakarta_7.html

I have gone through your blog, it was very much useful for me and because of your blog, and also I gained many unknown information, the way you have clearly explained is really fantastic. Kindly post more like this, Thank You.
Aviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
Medical coding training in Chennai
Fashion designing courses in Chennai
Interior design courses in Chennai

I am grateful to the owner of this site which really shares this wonderful work of this site.That is actually great and useful information.I'm satisfied with just sharing this useful information with us. Please keep it up to date like this.Thank you for sharing..
website designing company in patna
packers and movers in patna
cctv camera dealers in patna
jobs in patna

Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
Thanks & Regards,
VRIT Professionals,
No.1 Leading Web Designing Training Institute In Chennai.

And also those who are looking for
Web Designing courses training institutes in Chennai
HTML courses training institutes in Chennai
CSS courses training institutes in Chennai
Bootstrap courses training institutes in Chennai

Resources like the one you mentioned here will be very useful to me ! I will post to this page on my blog. I am sure my visitors will find that very useful

href="https://www.mytrainingbangalore.com/seo-training-in-bangalore/" rel="nofollow"SEO Training in Bangalore
href="https://www.mytrainingbangalore.com/" rel="nofollow"Best Training in Bangalore

hello sir,
thanks for giving that type of information. Really enjoyed this blog post. Really looking forward to reading more. Much obliged.
digital marketing company in delhi

Алюминиевый светодиодный профиль для led ленты я обычно беру в Ekodio там достойное качество и отличные цены.

I feel happy about and learning more about this topic. keep sharing your information regularly for my future reference. This content creates a new hope and inspiration with in me. Thanks for sharing article like this. the information which you have provided is better then other blog.
Best IELTS Coaching in Dwarka

You are doing a great job. I would like to appreciate your work for good accuracy.
devops training in chennai

Thanks for your post which gather more knowledge about this topic. I read your blog everytime which is helpful and effective.
Digital marketing training in chennai
Digital marketing course in chennai
SEO Training in chennai

I found a lot of valuable information in your article, it’s really helpful for me, am suggest to my all dear friend to visit this blog and collect this valuable information, I really happy to say this keep sharing this type of blog, I really like to read this, I hope I will see you again with this type of valuable information, thank you so much for read my comment I will suggest to you all comment reader please deeply read this blog and collect this useful information, if any one searching the website designing company in India please come on my website ogeninfo, we are working in 5 year on this field, we are complete your all type project like website designing & web development and PPC.
Website Designing Company in India

Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
SEO company in coimbatore
SEO company
web design company in coimbatore

Your blog give me a lot of valuable information, I really like to read your article, I will suggest to my all dear friend to visit this blog and collect this useful information, keep sharing like this type useful blog, I am really happy to say this am really enjoy to read this article, thank you so much for share this helpful information with us, if any one need a shipping company in India to shift you product, we are shift you product with care and faster if you need it so please come on my website Yhcargoindia, thanks to read my comment.
Sea Freight Company in Delhi

Very Clear Explanation. Thank you to share this
best java training institute in chennai

Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries.
SAS training in Chennai | SAS training in Chennai

Interesting information and attractive.This blog is really rocking... Yes, the post is very interesting and I really like it.I never seen articles like this. I meant it's so knowledgeable, informative, and good looking site. I appreciate your hard work. Good job.
Kindly visit us @
Sathya Online Shopping
Online AC Price | Air Conditioner Online | AC Offers Online | AC Online Shopping
Inverter AC | Best Inverter AC | Inverter Split AC
Buy Split AC Online | Best Split AC | Split AC Online
LED TV Sale | Buy LED TV Online | Smart LED TV | LED TV Price
Laptop Price | Laptops for Sale | Buy Laptop | Buy Laptop Online
Full HD TV Price | LED HD TV Price
Buy Ultra HD TV | Buy Ultra HD TV Online
Buy Mobile Online | Buy Smartphone Online in India

I love this post.

โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
เพียงแค่คุณสมัคร Gclub กับทางทีมงานของเราเพียงเท่านั้น
ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
สมัครสมาชิกที่นี่ >>> Gclub online

Very cool!

เว็บไซต์คาสิโนออนไลน์ที่ได้คุณภาพอับดับ 1 ของประเทศ
เป็นเว็บไซต์การพนันออนไลน์ที่มีคนมา สมัคร Gclub Royal1688
และยังมีเกมส์สล็อตออนไลน์ 1688 slot อีกมากมายให้คุณได้ลอง
สมัครสมาชิกที่นี่ >>> Gclub Royal1688

Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing ! Kindly Visit Us @ Best Travels in Madurai | Tours and Travels in Madurai | Madurai Travels

Interesting information and attractive.This blog is really rocking... Yes, the post is very interesting and I really like it.I never seen articles like this. I meant it's so knowledgeable, informative, and good looking site. I appreciate your hard work. Good job.
Kindly visit us @
Sathya Online Shopping
Online AC Price | Air Conditioner Online | AC Offers Online | AC Online Shopping
Inverter AC | Best Inverter AC | Inverter Split AC
Buy Split AC Online | Best Split AC | Split AC Online
LED TV Sale | Buy LED TV Online | Smart LED TV | LED TV Price
Laptop Price | Laptops for Sale | Buy Laptop | Buy Laptop Online
Full HD TV Price | LED HD TV Price
Buy Ultra HD TV | Buy Ultra HD TV Online
Buy Mobile Online | Buy Smartphone Online in India

Jasa Prin Warna Murah di Jakarta Timur I Rawamangun
http://www.idolaprinting.com/2018/09/tempat-print-warna-dan-hitam-putih.html

Thanks for sharing informative article… learning driving from experienced instructors help you to learn driving very fast… Learn driving lessons Melbourne from experts at Sprint driving School. Hazard Perception Test Practice Vic

Looking for latest update on TNPSC exams? Kalviseithi - #1 educational portal offer latest news about TN state government jobs, educational news and much more information.

Nice and interesting post. Thank You! For sharing such a great article, I like to read your information you have mentioned in this article are helpful for me.
IELTS coaching in Gurgaon

Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging
data science course malaysia
big data course malaysia
data analytics course malaysia
AI learning course malaysia
machine learning course malaysia
pmp certification malaysia

The article is very interesting and very understood to be read, may be useful for the people. I wanted to thank you for this great read!! I definitely enjoyed every little bit of it. I have to bookmarked to check out new stuff on your post. Thanks for sharing the information keep updating, looking forward for more posts..
Top 5 Website Development Company | Digital Marketing Company in Delhi

Very informative blog post! I would like to thank for the efforts you have made in writing this great article. Thanks for sharing.


Data Science Courses in Bangalore

The article is very interesting and very understood to be read, may be useful for the people. I wanted to thank you for this great read!! I definitely enjoyed every little bit of it. I have to bookmarked to check out new stuff on your post. Thanks for sharing the information keep updating, looking forward for more posts..
Kindly visit us @
Madurai Travels
Best Travels in Madurai
Cabs in Madurai
Tours and Travels in Madurai

Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
date analytics certification training courses
data science courses training
data analytics certification courses in Bangalore
ExcelR Data science courses in Bangalore

Welcome to the party of my life here you will learn everything about me.
Data Science Course in Pune

Wow, what an awesome spot to spend hours and hours! It's beautiful and I'm also surprised that you had it all to yourselves!
Kindly visit us @
Best HIV Treatment in India
Top HIV Hospital in India
HIV AIDS Treatment in Mumbai
HIV Specialist in Bangalore
HIV Positive Treatment in India
Medicine for AIDS in India

Thanks for this information
regards
https://saralearn.com/french-classes-in-bangalore/
https://saralearn.com/german-classes-in-bangalore/
https://saralearn.com/

I really enjoyed reading this post, big fan. Keep up the good work andplease tell me when can you publish more articles or where can I read more on the subject?





BIG DATA COURSE MALAYSIA

Nice and interesting post. Thank You! For sharing such a great article, I like to read your information you have mentioned in this article are helpful for me.
ielts center in gurgaon

Home Mart is a site about Home Improvement, Furniture, Home Appliances and many more.
Check out the best
furniture sale
Dog Cages
bedroom furniture nz
entertainment unit
shoe rack

Post a Comment