A brief description of reflection mechanism in. Net

Source: Internet
Author: User
Tags execution reference reflection valid

Overview Reflection

Reflection provides type information so that our developers can use that information to construct and use objects at run time.

The reflection mechanism allows the program to dynamically add various functions during execution.

Runtime type identification

Runtime type identification (RTTI), which can be used to determine the object type during program execution. For example, you can use it to know exactly what type of object The base class reference refers to.

Runtime type identification, which enables you to test a mandatory type conversion operation in advance to succeed, thereby avoiding invalid coercion type conversion exceptions.

There are three keywords in C # that support Rtti: is, as, and TypeOf. Let's introduce them in turn

Is operator:

The IS operator enables you to determine whether an object type is a top type, and if the two types are the same type, or if there is a reference between them, the boxed unboxing conversion indicates that the two types are compatible.

Class program
    {
        static void Main (string[] args)
        {
            A A = new A ();
            b b = new B ();
            If (A is a)  
            {
                Console.WriteLine ("a");   This print because A is an object of type a
            and
                This print because B is an object of type B, and B is derived from type A, because B objects can be converted to a type, so B objects are compatible with type A. But it does not, for example, not print
                Console.WriteLine ("B is A A because it's derived from");
            If (A is B)
            {
                //This does not print
                Console.WriteLine ("This won ' t display, because a isn't derived from B");
            If (A is object)
            {
                //This print
                Console.WriteLine ("A is an object");
            }
            Console.readkey ();
        }
    Class A {}
    class B:a {}

As operator:

Type conversions are performed during run time and can cause type conversions to fail without throwing exceptions and return a null value, in fact, as can also be considered as a simplified alternative to the IS operator (see example).

Class program
    {
        static void Main (string[] args)
        {
            A A = new A ();
            b b = new B ();
            If (A is B)
            {
                B = (b) A;   Because a variable is not of type B, it is not valid to convert a variable to type B.
            }
            else
            {
                b = null;
            }
   
            if (b ==null)
            {
                //This print
                Console.WriteLine ("The cast in b= (b) A is not allowed") 
            ;
   
            Using the as operator above, the two parts can be combined.
   
            B = A as B;   The as type first checks the validity of the coercion type conversion and, if it is valid, performs a strongly typed conversion procedure. These are all done in this sentence.
            if (b = = null)
            {
                //This print
                Console.WriteLine ("The cast in b= (b), A is not allowed");
            }
            Console.readkey ();
        }
    Class A {}
    class B:a {}

typeof operator:

As, is capable of testing two types of compatibility. But in most cases, you need to get specific information about a type. This uses the typeof, which can return the System.Type object associated with the specific type, and can be used to top this type of feature by System.Type object. Once you get a type object of a given kind, you can get specific information about the type by using the various properties, fields, and methods defined by the object. The type class contains many members, which are discussed in detail in the next reflection. The following is a simple demo type object that calls its three properties.

static void Main (string[] args)
        {
            Type t=typeof (StringBuilder);
   
            Console.WriteLine (t.fullname);  FullName property return type full name
   
            if (t.isclass)
            {
                Console.WriteLine ("is a Class");//Print
            }
            if (t.issealed)  //Whether it is a sealed class
            {
                Console.WriteLine ("Is Sealed");  Print
            }
            console.readkey ();
   
        

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

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.