C # -- pointing to this in the parent class and obtaining the Type of the current class with reflection

Source: Internet
Author: User

[Csharp] using System; using System. collections. generic; using System. linq; using System. text; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {Console. writeLine ("direct new Parent class"); Parent p = new Parent (); p. print (); Console. writeLine ("new subclass"); Sub s = new Sub (); s. print (); Console. writeLine ("Parent class new subclass"); Parent p2 = new Sub (); p2.Print (); Console. read () ;}} public Class Parent {public void Print () {Console. WriteLine ("1st types: Random strain. The child is the Child Parent and the Parent. Obtain the type "); Type t1 = this. getType (); Console. writeLine (t1.FullName); Console. writeLine ("2nd: it must be a parent class. obtain the type ") of the class where the execution method is located; Type t2 = new System. diagnostics. stackTrace (). getFrame (0 ). getMethod (). declaringType; Console. writeLine (t2.FullName); Console. writeLine ("3rd: it must be a parent class. obtain the type of the class object where the execution method is located.); Type t3 = System. reflection. methodBase. getCurrentMethod (). reflectedType; Console. writeLine (t3.FullName); Console. writeLine ("4th: it must be a parent class. obtain the type "); Type t4 = System. reflection. methodBase. getCurrentMethod (). declaringType; Console. writeLine (t3.FullName); Console. writeLine () ;}} public class Sub: Parent {}}

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.