C # obtain Caller information,

Source: Internet
Author: User

C # obtain Caller information,

When a log component is used, the caller information is often recorded. The caller information is usually obtained through reflection. However,. Net 4.5 introduces three new features: CallerFilePathAttribute, CallerLineNumberAttribute, and CallerMemberNameAttribute. These three features can only be applied to parameters and can only be used in applications and optional parameters. The concept is very simple: if the call point does not provide real parameters, the compiler can use the current file, number of lines, or member name as the real parameter, instead of using the regular default value. If the caller provides real parameters, the compiler ignores these features.

1 class ProgramCallerLineNumberAttribute 2 {3 static void Main (string [] args) 4 {5 Log. logWrite ("test"); 6 Console. readKey (); 7} 8} 9 10 class Log11 {12 public static void LogWrite (string logInfo, 13 [CallerFilePath] string file = null, 14 [CallerLineNumber] int line = 0, 15 [CallerMemberName] string member = null16) 17 {18 Console. writeLine (logInfo); 19 Console. writeLine (file); 20 Console. writeLine (line); 21 Console. writeLine (member); 22 23} 24}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.