扩展方法的使用 MVC

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace Mvc2App.Tools

{

    public static class HtmlHelpers

    {

        public static string SubString(this HtmlHelper html,string InputStr,int Count)

        {

            if (InputStr.Length < Count)

            {

                return InputStr;

            }

            else

            {

                return InputStr.Substring(0, Count) + ".....";

            }

        }

    }

}

以上是对常用的HtmlHelper类进行的一个扩展,需要对类进行相应的扩展,需满足几个条件,被扩展的方法是静态的,新扩展的方法类是静态类,方法也是静态的。

使用方法:

 

  1)引用扩展方法所在的命名空间。

  

 

2)使用扩展方法:

 

 

类所在的命令空间结构如下所示:

 

 

 

 

 

 

 

posted @ 2013-04-07 19:21  Wsf5918  阅读(133)  评论(0编辑  收藏  举报