C # generics and reflection

Source: Internet
Author: User

1. Generic set:

List Dictionary IQueryabl

2. Why is ArrayList not recommended?

2.1 because ArrayList stores data in an array of objects. If the value type data is stored in ArrayList, packing and unpacking may occur, affecting server performance.

2.2 type security problem: because all elements in the ArrayList are stored as objects, the elements in the ArrayList are also retrieved as objects, which requires transformation. If this problem is not solved, A type error may occur!

3. Generic: it isType placeholder

3.2 generic Method

3.2.1 custom generic Method

In a generic method, you can use a generic parameter, a returned value, or a method body.

3.2.2IEnumerabl Built-in generic Method

A. Method Cast ()

B. Method ToList ()

C. Method OfType ()

3.3 differences between a class with generics and a method with generics:

Wildcard placeholders use different scopes:

Generic placeholders of generic methods can only be used in method scopes (Form parameters, return values, and method bodies)

Generic class: the scope of use is the scope of the entire class.

Reflection

1. Assembly: it is the intermediate code file after the source code is compiled into IL.

2. When the program is executed,

2.1 The virtual opportunity loads the assembly code content from the hard disk, parses the code according to the code syntax, obtains information, and creates a Type Class Object for each class separately, used to store class information (member variables, constructors, methods ............).

Review reflection:

The Assembly object is equivalent to the location where the Assembly file content is parsed and stored in the memory.

In the end, it is a container of program information and cannot be directly run as an application. If you want to run the Assembly, you need to use the application domain.

1. How to obtain the Type object:

1.1 typeof Keyword: Type t1 = typeof (Person );

1.2GetType method: Type t2 = p1.GetType (); // Person p1 = new Person ();

Note: The two methods obtain the same Type object (t1 = t2 ).

2. Feature Attribute

Custom feature class: inherits from Attribute class;

The class name ends with Attribute;

Attribute after the class can be omitted during use.

Call the member fields and methods of an object Using Reflection:

In the past, we ran it through object. Field and object. method;

Reflection can be used to run fields, objects, methods, and objects;

Person p = new Person ();

Public void InvokePerson (Person pPara)

{

Type pType = typeof (Person );

FieldInfo fInfo = pType. GetField ("age ");

String strAge = fInfo. GetValue (pPara). ToString ();

MethodInfo mInfo = pType. GetMethod ("SayHi ");

MInfo. Invoke (pPara, null );

}

Generic can be empty:

Nature of delegation:

It is a class that inherits from the MulticastDelegate class (Multicast Delegate) and the Delegate class (Delegate ).

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.