在静态类扩展方法? [英] Extension methods on a static class?

查看:103
本文介绍了在静态类扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
我可以添加扩展方法,现有的静态类?






我知道我可以做下面的扩展类。我有一个静态类我想延长。我会如何做呢?我想编写 ClassName.MyFunc()

 的静态公共类SomeName 
{
静态公共INT HelperFunction(这个SomeClass的v)


解决方案

您不能在静态类扩展方法,因为扩展方法
只适用于实例化
类和静态类不能
实例化。




勾选此代码..

 公共静态布尔ISEMAIL(此字符串电子邮件)
{
如果(电子邮件!= NULL)
{
返回Regex.IsMatch (电子邮件,EmailPattern);
}

返回FALSE;
}



第一个参数为 ISEMAIL()与延伸型实例,并且不只是类型本身。你永远不能拥有一个静态类型的实例。


Possible Duplicate:
Can I add extension methods to an existing static class?

I know i can do the below to extend a class. I have a static class i would like to extend. How might i do it? I would like to write ClassName.MyFunc()

static public class SomeName
{
    static public int HelperFunction(this SomeClass v)

解决方案

You can't have extension methods on static classes because extension methods are only applicable to instantiable types and static classes cannot be instantiated.

Check this code..

    public static bool IsEmail(this string email)
    {
        if (email != null)
        {
            return Regex.IsMatch(email, "EmailPattern");
        }

        return false;
    }

First parameter to IsEmail() is the extending type instance and not just the type itself. You can never have an instance of a static type.

这篇关于在静态类扩展方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆