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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

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
3.51K 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

  • Version Number Anti-Patterns
  • What's the Difference Between Static Class vs. Singleton Patterns in C#?
  • Python 101: Equality vs. Identity
  • Terraform Controller: Cloud Resource Self-Service

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo