DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Caller Info Attributes in C# 5 Are a Framework-Agnostic Compiler Feature

Toni Petrina user avatar by
Toni Petrina
·
Mar. 31, 12 · Interview
Like (0)
Save
Tweet
Share
3.67K Views

Join the DZone community and get the full member experience.

Join For Free

the new caller information attributes in c# 5 are excellent news for people tired of writing logging and diagnostic code or implementing inotifypropertychanged. you simply supply the correct attribute for the function parameters with default values and voilà – you get information about the caller for free. consider the following code:

using system;
using system.runtime.compilerservices;

class program
{
    static void saymyname([callermembername] string functionname = "",
                          [callerfilepath] string filepath = "",
                          [callerlinenumber] int linenumber = 0)
    {
        console.writeline("{0}:{1}({2})", filepath, functionname, linenumber);
    }

    static void main(string[] args)
    {
        saymyname();
    }
}

the output of this simple program is:

c:\users\toni\documents\visual studio 11\projects\test_console\cstest\program.cs:main(15)

excellent, we have the caller’s information for free. .net 4.5 framework introduced the above used attributes: callermembernameattribute, callerfilepathattribute and callerlinenumberattribute. they are defined in the system.runtime.compilerservices namespace. this means that if you are using visual studio 11 and you are targeting earlier framework, you do not have those types defined in mscorlib.dll. however, even though this might make you think that you are left in the dark, this is a compiler feature which means that these attributes are nothing but a magic keywords for the compiler. they themselves do nothing!

in fact, i have compiled the above program targeting .net 2.0 framework, the earliest target type available in visual studio 11. the trick is to add the right “magic” attributes and it will work since the compiler will interpret them correctly. the full source code is rather short and you can find it as a gist at github .

i based this idea on a similar “hack” for using async in projects targeting .net 4 on the c#5 compiler: https://gist.github.com/1961087 .

Attribute (computing)

Published at DZone with permission of Toni Petrina, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Secure APIs: Best Practices and Measures
  • OWASP Kubernetes Top 10
  • Building a Real-Time App With Spring Boot, Cassandra, Pulsar, React, and Hilla
  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: