Use the. net extension method to make encoding elegant

Source: Internet
Author: User

As the first technical Article of the technology blog, I thought a little hypocritical at the beginning: I am a little reluctant to be considered to write such a "Basic" article. There are many cool people in the garden.

As if the software world is like a curse, you can't understand the code, it's someone else's cow. You knew the method was someone else's dish...

Well, it seems that the biggest reason for my determination to write these articles is to help some friends solve some practical problems during the development process.

So it doesn't matter. Han has the courage to collect 5000 of the most vicious comments. What's more, I am an unknown pawn...

To put it bluntly, one day, kakim saw an intern working on a page and wanted to convert some time and decimal special formats,

After reading about it, The CodeBehind file found a large number of code similar to the following:

This. hdCheckInDate. Value = DateTime. Parse (dt. Rows [0] ["arrivalDate"]. ToString (). ToString ("yyyy-MM-dd ");

This. liAveRate. Text = decimal. Parse (dt. Rows [0] ["roomRate"]. ToString (). ToString ("#,##. 00 ");

Then I checked all the page binding lists such as the GridView or Repeater, and found several pages are similar.

<% # Convert. ToDateTime (Eval ("arrivalDate"). ToString ("yyyy-MM-dd") %>

 

Such repeated conversions can simplify their work because there are many similar pages in the project and the project time is tight,

So I remembered the extension method of. NET and wrote an extension class.

 

 Namespace System
{
Public static class ExtensionString
{
/// <Summary>
/// Determine the null value of a string
/// </Summary>
/// <Param name = "value"> </param>
/// <Returns> </returns>
Public static bool IsNullOrEmpty (this String value)
{
If (value! = Null)
Return (value. Length = 0 );
Return true;
}

/// <Summary>
/// Conversion time and decimal number
/// </Summary>
/// <Param name = "obj"> </param>
/// <Returns> </returns>
Public static string ToExtensionString (this Object obj)
{
String s = string. Empty;
If (obj = null) return s;
If (obj is DateTime)
S = Convert. ToDateTime (obj). ToString ("yyyy-MM-dd ");
Else if (obj is Decimal)
S = Convert. ToDecimal (obj). ToString ("###, ## 0.00 ");
Else
S = obj. ToString ();
Return s;
}
}
}

Of course, here, the ToExtensionString () method is relatively simple in business processing, and everyone can handle it flexibly. However, for the code of most repetitive work of the entire project, this reduces unnecessary troubles,

Introduce the extension class in the website project, and the intern will find that the ToExtensionString () Extension Method is added every time the list binding and conversion is created on the page...

Therefore, we can make the above encoding into the following "concise"

This. hdCheckOutDate. value = dt. rows [0] ["departureDate"]. toExtensionString (); this. liAveRate. text = dt. rows [0] ["roomRate"]. toExtensionString ();

On the page, you can directly write the content in the list of all the GridView or Repeater tables.

<% # Eval ("arrivalDate"). ToExtensionString () %>

<% # Eval ("roomRate"). ToExtensionString () %>

 

Of course, as you can imagine, many friends can write a lot of extensions, such as character filtering. You can also use this method to expand most types of extensions ~~

However, there are still rules for extension methods:

1. If the instance method and the extension party have the same signature, the instance method is called.

2. If the two extension methods have the same signature, a method must be called as a normal static method.

3. The static class to which the extension method belongs must be in the scope. Otherwise, the method cannot be called.

Give full play to your imagination. Maybe you will write more powerful code ~~

 

 

 

 

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.