C# 实现和调用自定义扩展方法
定义和调用扩展方法
- 定义一个静态类以包含扩展方法。该类必须对客户端代码可见。
- 将该扩展方法实现为静态方法,并使其至少具有与包含类相同的可见性。
- 该方法的第一个参数指定方法所操作的类型;该参数必须以 this 修饰符开头。
- 在调用代码中,添加一条 using 指令以指定包含扩展方法类的命名空间。
- 按照与调用类型上的实例方法一样的方式调用扩展方法。
请注意,第一个参数不是由调用代码指定的,因为它表示正应用运算符的类型,并且编译器已经知道对象的类型。 您只需通过 n 为这两个形参提供实参。
示例
下面的示例在 CustomExtensions.StringExtension 类中实现了一个名为 WordCount 的扩展方法。
该方法对 String 类进行操作,而该类被指定为第一个方法参数。
CustomExtensions 命名空间被导入到应用程序命名空间中,并且该方法是在 Main 方法内调用的
using System.Linq;
using System.Text;
using System; namespace CustomExtensions
{
//Extension methods must be defined in a static class
public static class StringExtension
{
// This is the extension method.
// The first parameter takes the "this" modifier
// and specifies the type for which the method is defined.
public static int WordCount(this String str)
{
return str.Split(new char[] {' ', '.','?'}, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}
namespace Extension_Methods_Simple
{
//Import the extension method namespace.
using CustomExtensions;
class Program
{
static void Main(string[] args)
{
string s = "The quick brown fox jumped over the lazy dog.";
// Call the method as if it were an
// instance method on the type. Note that the first
// parameter is not specified by the calling code.
int i = s.WordCount();
System.Console.WriteLine("Word count of s is {0}", i);
}
}
}
C# 实现和调用自定义扩展方法的更多相关文章
- Jquery自定义扩展方法(二)--HTML日历控件
一.概述 研究了上节的Jquery自定义扩展方法,自己一直想做用jquery写一个小的插件,工作中也用到了用JQuery的日历插件,自己琢磨着去造个轮子--HTML5手机网页日历控件,废话不多说,先看 ...
- AbpVnext使用分布式IDistributedCache Redis缓存(自定义扩展方法)
AbpVnext使用分布式IDistributedCache缓存from Redis(带自定义扩展方法) 我的依赖包的主要版本以及Redis依赖如下 1:添加依赖 <PackageReferen ...
- Jquery自定义扩展方法(一)
jquery是一款流行的JS框架,自定义JS方法,封装到Jquery中,调用起来也挺方便的,怎么写Jquery扩展方法那,网上翻阅了一部分代码,其实也挺简单的: 方式一: (jQuery.fn.set ...
- WinForm TextBox自定义扩展方法数据验证
本文转载:http://www.cnblogs.com/gis-crazy/archive/2013/03/17/2964132.html 查看公司项目代码时,存在这样一个问题:winform界面上有 ...
- 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法
比如Json.NET的JObject明明实现了IEnumerable<T>,具体来说是IEnumerable<KeyValuePair<string, JToken>&g ...
- Unity中自定义扩展方法
问题背景 在使用unity开发过程中,通常会遇到一种情况,比如说给物体重新赋值坐标的问题, Transfrom tran: ,pos_y=,pos_z=; tran.position=new Vect ...
- 【手记】走近科学之为什么JObject不能调用LINQ扩展方法
Json.NET的JObject明明实现了IEnumerable<T>,具体来说是IEnumerable<KeyValuePair<string, JToken>> ...
- Layui数据表格加入自定义扩展方法(重新渲染Render当前页数据)
具体开发中遇到的问题如下, 数据表格的重新渲染或重新加载会导致当前操作的分页 或 配置被清空.我正在操作第5页,重新渲染后就回到了最原始第1页. 需要达到的效果是: 不调用接口,仅仅只是从table. ...
- 在使用EF时调用DBFUNCTION扩展方法时,发生ENTITYCOMMANDEXECUTIONEXCEPTION 异常的解决办法
System.Data.Entity.Core.EntityCommandExecutionException : An error occurred while executing the comm ...
随机推荐
- Linux服务器之SSH
SSH 1.ssh是安全的加密协议,用于远程连接linux服务器. 2.ssh默认端口是22,安全协议版本ssh2. 3.ssh服务端主要包含两个服务功能ssh远程连接,sftp服务. 4.linux ...
- HDU3308 LCIS
Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Description Given n ...
- MSClass (Class Of Marquee Scroll通用不间断滚动JS封装类) Ver 1.65
原文发布时间为:2010-02-07 -- 来源于本人的百度文章 [由搬家工具导入] http://www.popub.net/script/MSClass.html/*MSClass (Class ...
- [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- duilib入门简明教程 -- 第一个程序 Hello World(3) (转)
原文转自 http://www.cnblogs.com/Alberl/p/3343579.html 小伙伴们有点迫不及待了么,来看一看Hello World吧: 新建一个空的win32项目,新建一个m ...
- Win32 OpenGL 编程( 1 ) Win32 下的 OpenGL 编程必须步骤
http://blog.csdn.net/vagrxie/article/details/4602961 Win32 OpenGL 编程( 1 ) Win32 下的 OpenGL 编程必须步骤 wri ...
- 無法使用 system/bin/r 讀取 pmic pm8937 hardware regitster 的原因
Platform Qualcomm MSM8917 + PM8937 + PMI8940 起因 同事問我 PM8937 的 VREG_L17 如何設定成 3.3V, 從 PM8937 hardware ...
- 在Android 7.0以上PopupWindow.showAsDropDown()不起作用问题
最近优化界面,调用PopupWindow.showAsDropDown()发现,窗口并没有在控件下方显示. 有网友说是7.0以上系统的问题,可以自定义PopupWindow,重写showAsDropD ...
- python中mp3转wav(Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work")
1.下载pydub:pip install pydub 2.下载与操作系统一致的ffmpeg:http://ffmpeg.org/download.html 3.解压后将下载的ffmpeg下的bin目 ...
- 2016北京集训测试赛(十三) Problem B: 网络战争
Solution KD tree + 最小割树