C # FAQ in Type Analysis

Source: Internet
Author: User

WriteCodeYou often need to analyze existing types of information.

For example, analyze existing types to automatically generate classes, or automatically add some functions to existing classes.

Summarized a little experience

Take classa A; as an Example

1. You can use typeof (classa) or A. GetType () to obtain the type information. We recommend that you use typef () to avoid null references, and sometimes you do not need to construct a classa instance,

The performance of typeof (classa) is not poor at all. Do not confuse it with reflection,

2. For type. isprimitive, many common types are not native types.

For example, string datetime decimal is not of the native type.

According to msdn instructions native types only include: http://msdn.microsoft.com/en-us/magazine/bb984984.aspx

3. For arrays, typeof (classa [])

You can use type. isarray to determine whether an array is used.

You can use type. getelementtype to obtain the element type, which is equivalent to typeof (classa)

Arrays actually inherit from system. array, but arrays also implement the ienumerable <t> interface (this allows common types such as array and list <t> to be processed in the same way)

3. For generics, typeof (list <classa>)

You can use type. isgenerictype to determine whether it is a generic type.

You can use type. getgenerictypedefinition () to obtain the generic type (Open Type), which is equivalent to typeof (list <>)

Use type. getgenericarguments () to obtain the generic parameter set. The first element in the set is equivalent to typeof (classa)

For dictionary definitions such as Dictionary <string, classa>, type. getgenericarugment () returns two types: typeof (string) and typeof (classa). Its open type is typeof (Dictionary <,>)

4. You can call type. getinterfaces () to determine whether a type inherits from an interface.

For example, both array and list <t> inherit from ienumerable <t>, so that they can be processed in a unified manner.

5. Can the null value type be Int? Its open type is typeof (system. nullable <>)

The Open Type here refers to an incomplete type. You need to add parameters (generally generic parameters) to form a real type, such as list <>, added the wildcard parameter string to list <string>

Open type is not a complete type. You cannot create an instance directly.

The closed type is already a complete type. You can use new ..

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.