. Net reflection (1)

Source: Internet
Author: User

Http://service.ap-southeast-1.maxcompute.aliyun-inc.com.

In my article C # interface-based programming, I talked about the advantages of using abstract interfaces as programming examples. Separating interfaces and execution processes is certainly not a new idea. In fact, it is the core of com programming. Perhaps the significant characteristics of the interface-based approach are polymorphism and instant check (RTTI). RTTI allows client programs to access objects at runtime. For example, if an object executes the IAthlete interface, a client program can search for and bind this interface to call and locate another interface.

The query interface is powerful and is the foundation of com +. At the same time, it is extremely useful and necessary to execute interface orientation on an object, which is a particularly important concept of. net. Before introducing this concept, let's review some information about the basic elements of functionality and distribution in. net-set.

In COM (+), the logic and physical unit of component expansion is a. dll file. On the. net platform, the basic unit to expand is a set. Unlike the COM (+) component, A. net component may consist of multiple files, although it is regarded as a single function and execution unit. A collection file is also self-described. It contains the control code and additional metadata called the intermediate language. This metadata provides a wealth of information for customers who want to bind the code .. Net is a well-known list data structure. The list contains the following information:

1. Name and version information

2. reference information for other sets

3. Security boundary information.

4. All types of information in the collection

There is no doubt that the collection list is similar to the type library of COM (+. The list has the advantage that it does not exist in the Type Library. This is beyond the scope of this lesson and will be discussed in the second part of this article. The list will be used by client programs using the. net collection. Because each set contains a lot of metadata information, the customer program can get the set content at runtime. This process is called reflection. Reflection, which can not only query basic classes, but also dynamically call methods during runtime, and even output IL code that can be directly executed. There are two methods in your code to use reflection, Reflection API and System. Reflection namespace. System. reflection is extremely responsible and contains nearly 40 classes. In other sections of the article, I will introduce the basics of reflection, how to use it to query collection classes, methods, and types of methods called at runtime. To demonstrate the reflection namespace in. net, I will use the following "reflection" class to describe:

// Reflect. cs

Namespace CSharpReflectionSamples

{

Using System;

/// <Summary>

/// The reflect class will be used to demonstrate the basics

///. NET reflection. This class contains private member vars,

/// A constructor, a sample method, and sample accessor methods.

/// </Summary>

Public class Reflect

{

// Private member variables

Private int intTest;

Private string strTest;

// Constructor

Public Reflect (int intData)

{

// Constructor logic

IntTest = intData;

}

// Basic method

Public int AMethod (int intData)

{

Return intData * intData;

}

Public string S

{

Get

{

// Return member var

Return strTest;

}

Set

{

// Set member var

S = value;

}

}

}

}

As you can see, this class contains a constructor, an example method, and an example access method (get and set ). At the core of the System. Reflaction namespace is a class named type. This type contains many methods and a logon entry to many class information. The Type class can be obtained by using the typeof or GetType method. The following table lists some methods of the class type, including the code snippet of the example GetTypeof and typeof methods.

Name

Description

GetFields ()

Returns an array of FieldInfo objects describing the fields in the class.

GetMethods ()

Returns an array of MethodInfo objects describing the methods of the class.

GetConstructors ()

Returns all the constructors for an object.

GetInterfaces ()

Gets all interfacs implemented by the type.

GetMembers ()

Gets all the members (fields, methods, events, etc.) of the current type.

InvokeMember ()

Invokes a member of the current type.

BaseType

Gets a Type object for the types immediate base type.

IsAbstract

Returns true if the type is abstract.

IsClass

Returns true if the type is a class.

IsEnum

Returns true if the type is an enum.

IsNestedPublic

Returns true if a type is nested within another and is public.

Namespace

Retrieves the namespace of the type.

Namespace CSharpReflectionSamples

{

Using System;

Using System. Reflection;

/// <Summary>

/// Summary description for Client.

/// </Summary>

Public class Client

{

Public static void Main ()

{

// The typeof operator and the GetType method

// Both return a Type object.

Type type1 = typeof (Reflect );

Reflect objTest = new Reflect (0 );

Type type2 = objTest. GetType ();

Console. WriteLine ("Type of objTest is {0}", type2 );

}

}

}


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.