. NET Reflection (1)

Source: Internet
Author: User
Tags abstract bind constructor contains execution query reflection
In my article, "C # interface-based programming," I talked about the advantages of using an abstract interface as a programming paradigm. The separation of interfaces and the execution process is now definitely not a new idea, but it is actually the core of COM programming. Perhaps the interface based in the salient feature is polymorphism and instant check (RTTI). RTTI allows the client program to access the object at run time. For example, if an object executes the Iathlete interface, a client program can find and then bind the interface to invoke and locate another interface.
The query interface is powerful and is the foundation of COM +. At the same time, it is extremely useful and clearly necessary to be able to perform interface-oriented on an object, which is a particularly important concept. Net. Before introducing this concept, let's review some of the information about the basic elements of functionality and distribution in. NET-collections.
in COM (+), the logical and physical unit that the component expands is a. dll file. In the. NET platform, the expanded base unit is a collection. Unlike a COM (+) component, a. NET may consist of multiple files, although it is seen as a single, indivisible function and execution unit. A collection file is also self-describing, containing the control code known as the intermediate language and additional metadata, which provides rich information to customers who want to bind to the code. Each collection of. NET is a well-known inventory data structure. The list contains the following information:
1, name and version information
2, other sets of reference information
3. Information on security boundaries.
4, all types of information in the collection

There is no doubt that the collection list is similar to a COM (+) type library. The manifest has advantages that do not exist in the type library. This is beyond the scope of this lesson and we will cover the second part of this article. A client program that is using the. NET collection will use the manifest. Because each collection contains a lot of information about the metadata, the client program can get the contents of the collection at run time. This process is called reflection. Reflection not only can query the underlying class, but the runtime can dynamically invoke methods, even the output of IL code that can be executed directly. There are two ways to use the Reflection,reflection API and the System.Reflection namespace in your code. System.Reflection is extremely responsible, and it contains nearly 40 classes. In the rest of the article, I'll explain the basics of reflection, how to use it to query collection classes, methods, and the types of methods that the runtime invokes. To demonstrate the reflection namespace in. NET, I will use the following "reflection" class to illustrate:

Reflect.cs
Namespace Csharpreflectionsamples
{
Using System;

<summary>
The reflect class is used to demonstrate the basics of
. NET Reflection. This class is 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, an example accessor method (get and set). In the System.reflaction namespace the core is a class called type. This type contains a number of methods and a login entry to many classes of information. Type class references can be obtained by means of TypeOf or GetType. The following table lists the methods for some class types, including code snippets for sample 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 to an object.

Getinterfaces ()
Gets all Interfacs implemented by the type.

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

InvokeMember ()
Invokes A is the current type.

BaseType
Gets a type object for the type ' s 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 are 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);

}
}
}



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.