In the vs environment, we occasionally or even frequently write extension methods in the application so that we can "click" until our goal, which saves time and effort, although there is a small amount of resource consumption, this is very useful for me, a lazy person !!!
For example, I often extend the string method, for example, directly intercepting the string method:
Public static string getsubstr (this string orgstr, int length ){
Return orgstr. length> length? Orgstr. substring (0, length): orgstr;
}
Or determine whether it is a numeric method.
Public static bool isnumeric (this string orgstr ){
If (orgstr. isnullorempty ()){
Return false;
}
Foreach (char s in orgstr ){
If (S <48 | S> 57 ){
Return false;
}
}
Return true;
}
Alternatively, you can use the following method to determine whether the string is null:
Public static bool isnullorempty (this string orgstr ){
Return string. isnullorempty (orgstr );
}
...... And so on.
However, this is not always a problem. I am in trouble to use these methods on the ASPX page. for example, when you bind a data list, we usually want to display it in a row for the sake of appearance. If there are too many words, we can write the following statement in repeater:
Traditional (with a limit of 30 characters ):
<% # Eval ("subject"). tostring (). length> 30? (String) eval ("subject"). substring (0, 30): eval ("subject") %>
After using the extension method:
<% # Eval ("subject"). tostring (). getsubstr (30) %>
The number of words is simplified. If there are n lists on the homepage of an enterprise site, you can see that many words are saved!
However, the problem is that the extension method is not defined. In theory, the ASPX page inherits the Aspx. CS class, so Aspx. the CS class references XXXX. bll namespace, so aspx should also be able to request, but in fact the request is not available, so we have to add the following code on the second line at the top of the page: <% @ import namespace = "XXXXX. bll "%>
Debugging and running, successful!