C # Caller Information acquisition

Source: Internet
Author: User

When a log component is made, the caller information is often logged, usually through reflection to obtain the appropriate information. However,. Net 4.5 introduces three new features, Callerfilepathattribute,callerlinenumberattribute and Callermembernameattribute. These three attributes can only be applied to parameters and are only useful when applied with optional parameters. The idea is simple: if the calling point does not provide an argument, the compiler can use the current file, the number of rows, or the member name as the argument, instead of using the regular default value, and the compiler ignores these attributes if the caller provides the arguments.

1 classProgramcallerlinenumberattribute2     {3         Static voidMain (string[] args)4         {5Log.logwrite ("Test");6 Console.readkey ();7         }8     }9 Ten     classLog One     { A          Public  Static voidLogwrite (stringLoginfo, -[Callerfilepath]stringfile=NULL, -[Callerlinenumber]intLine=0, the[Callermembername]stringMember=NULL -             ) -         { - Console.WriteLine (loginfo); + Console.WriteLine (file); - Console.WriteLine (line); + Console.WriteLine (member); A  at         } -}

C # Caller Information acquisition

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.