zoukankan      html  css  js  c++  java
  • C# 扩展系统类方法

    1、声明扩展方法的步骤:类必须是static,方法是static

    2、第一个参数是被扩展的对象,前面标注this

    3、使用扩展方法的时候必须保证扩展方法类已经在当前代码中using

    例子:
    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    namespace CLeopard.SystemExt { //扩展方法必须是静态的 public static class StringHelper { //扩展方法必须是静态的,第一个参数必须加上this public static bool IsEmail(this string _input) { return Regex.IsMatch(_input, @"^w+@w+.w+$"); } //带多个参数的扩展方法 //在原始字符串前后加上指定的字符 public static string Quot(this string _input, string _quot) { return _quot + _input + _quot; } } } 使用: string _myEmail = "test@163.com"; //这里就可以直接使用string类的扩展方法IsEmail了 Console.WriteLine(_myEmail.IsEmail()); //调用接收参数的扩展方法 Console.WriteLine(_myEmail.Quot("!"));
    复制代码

    以上是基于扩展类的动态,那么静态方法如何去扩展,经过研究,C#4.0的语法没办法时间,不排除以后会出现实现方式,只能是普通建另一个类来做。

  • 相关阅读:
    Jquery中的bind()方法绑定事件总结
    composer常用命令
    Activity四种启动模式
    谷歌搜索技巧
    关于Android studio Haxm加速器安装
    关于Ping和Tracert命令原理详解
    皮尔逊相关系数
    head标签
    wireshark抓包
    数据结构与算法自学系列之动态规划(一)
  • 原文地址:https://www.cnblogs.com/longlongogo/p/6671912.html
Copyright © 2011-2022 走看看