Unknown C # basics 1. -- Extension Method

Source: Internet
Author: User

 

Background

A few days ago, a colleague asked me how to add a description string to each element and read it easily when enumeration is used.

 

For example, an enumeration type is used to list the options provided for a question (for example, agree? Disagree? Neutral ?) :

Public enum AssessmentAnswer

{

Strongly_Disagree = 1,

Disagree = 2,

Neutral = 3,

Agree = 4,

Strongly_Agree = 5

}

 

When selecting different answers, you want to get some descriptive statements, such:

1. "strong opposition"

2. "objection"

3. "neutral opinion"

4. "agree"

5. "full consent"

 

Of course, you can create a database table to store this information, but I remember a few years ago, I downloaded an EnumDescription source code online (I forgot where I downloaded it ), this function can be implemented.

Let's take a look at how Extension is used:

 

Implementation

First, define an EnumDescription class:

 

 

Public class EnumDescription: Attribute

{

Public string Text

{

Get {return _ text ;}

} Private string _ text;

 

Public EnumDescription (string text)

{

_ Text = text;

}

}

Note that its parent class is Attribute, because we want to use the described statement as a feature of each corresponding element. Then we create a file named EnumExtensions. cs.

Public static class EnumExtensions

{

Public static string ToDescription (this Enum enumeration)

{

Type type = enumeration. GetType ();

MemberInfo [] memInfo = type. GetMember (enumeration. ToString ());

 

If (null! = MemInfo & memInfo. Length> 0)

{

Object [] attrs = memInfo [0]. GetCustomAttributes (typeof (EnumDescription), false );

If (null! = Attrs & attrs. Length> 0)

Return (EnumDescription) attrs [0]). Text;

}

 

Return enumeration. ToString ();

}

}

 

Here we define an extended function ToDescription. Just like all the extended functions, its parameters are similar to (this ...),

This function first obtains the type of the current enumeration using GetType. Then, GetMember obtains the specific element based on the element name (value), and finally obtains the description using GetCustomAttributes.

 

After this extension function is implemented, we can modify the enumeration definition and add the description:

 

Public enum AssessmentAnswer

{

[EnumDescription ("strong opposition")]

Strongly_Disagree = 1,

[EnumDescription ("against")]

Disagree = 2,

Neutral = 3,

Agree = 4,

[EnumDescription ("full consent")]

Strongly_Agree = 5

}

 

To obtain the description statement, you can easily call ToDescription:

 

// Return "strong objection"

AssessmentAnswer. Strongly_Disagree.ToDescription ()

 

Note: If you do not add features to an element, you can still use ToDescription. Refer to the above Code to see why.

// Return "Disagree"

AssessmentAnswer. Disagree. ToDescription ()

 

Summary

As a special static method, extension methods allow you to "add" methods to an existing type without creating a new derived type, re-compiling, or modifying the original type in other ways.

For client code written in C # and Visual Basic, there is no significant difference between calling an extension method and calling a method actually defined in the type.

 

For beginners: Once you implement the extension method, you only need to copy the dll to add the file namespace, or copy the cs file to your project, you can use these methods, however, we recommend that you implement the extension method unless you have sufficient reasons.

 

Many of the extension methods we need can be obtained online. Therefore, you can search for them before writing your own extensions.

This article is from ASP. NET (Alex Song) of joy. Please indicate the source

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.