类型扩展方法 [英] Extension method on type

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

问题描述

是否可以为类型创建扩展方法?我似乎只能为实例创建它们.

Is there a way to create an extension method for an type ? I only seem to be able to create them for instances.

public static class MyExtensions
{
    public static string Test(this string s)
    {
        return "test";
    }
}

public class Test
{
    static void TestIt()
    {
        string.Test();  // won't compile

        string s = null;
        s.Test();
    }
}

推荐答案

否,这是不可能的.扩展方法只能为实例创建

No, it's not possible. Extension methods can only be created for instances

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

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