扩展方法必须在非泛型静态类中定义
2016-07-27 10:25
316 查看
扩展方法必须在非泛型静态类中定义:
1、扩展方法即静态方法。
2、扩展方法必须在静态类里面。
public class CustomerHelperClass { public static MvcHtmlString CreateImage(string imageSource, string altText, string width, string height) { //通过TagBulider创建标签 TagBuilder imageTag = new TagBuilder("img"); //MergeAttribute添加新特性 imageTag.MergeAttribute("src", imageSource); imageTag.MergeAttribute("alt", altText); imageTag.MergeAttribute("width", width); imageTag.MergeAttribute("height", height); //MvcHtmlString.Create使用指定的文本值,创建HTML编码的字符串 return MvcHtmlString.Create(imageTag.ToString(TagRenderMode.SelfClosing)); } public static MvcHtmlString CreateImage(this HtmlHelper htmlHelper, string imageSource, string altText, string height, string width) { //通过TagBuilder创建标签 TagBuilder imageTag = new TagBuilder("img"); imageTag.MergeAttribute("src",imageSource); imageTag.MergeAttribute("alt", altText); imageTag.MergeAttribute("height", height); imageTag.MergeAttribute("width", width); //MvcHtmlString.Create使用指定的文本值,创建HTML编码的字符串 return MvcHtmlString.Create(imageTag.ToString(TagRenderMode.SelfClosing)); } }在上面的这段代码中在编译时会出现“扩展方法必须在非泛型静态类中定义”,出现错误的原因就在于
public class CustomerHelperClass扩展方法所在的这个类为非静态的,只需将扩展方法所在的类定义成静态类即可,修改如下:
public static class CustomerHelperClass因此在使用静态方法时需要注意:
1、扩展方法即静态方法。
2、扩展方法必须在静态类里面。
相关文章推荐
- 表格标签table深入了解
- WEB标准网页布局中尽量不要使用的HTML标签
- 如何使用jquery easyui创建标签组件
- Ruby 之 class 中的 private、 protected、public
- web标准知识――用途相似的标签
- h1标签的使用技巧
- Ruby中的public、private、protected区别小结
- 探讨JavaScript标签位置的存放与功能有无关系
- HTML代码中标签的全部属性 中文注释说明
- 第九章之路径分页标签与徽章组件
- javascript实现label标签跳出循环操作
- Bootstrap每天必学之标签与徽章
- jQuery EasyUI 布局之动态添加tabs标签页
- jQuery+CSS实现滑动的标签分栏切换效果
- jquery 实现两Select 标签项互调示例代码
- 详解jQuery Mobile自定义标签
- jQuery获取标签文本内容和html内容的方法
- JavaScript获取并更改input标签name属性的方法
- css样式标签和js语法属性区别
- JSP自定义标签入门学习