添加扩展方法的方法? [英] Way to add an Extension method?

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

问题描述

说我有一个名为 LogHelper 的类,它有一个名为 GetLogger(string name)的静态方法.

Say I have a class called LogHelper and it has a static method called GetLogger(string name).

是否有一种方法可以添加仅是 GetLogger()的静态扩展方法?

Is there a way to add a static extension method that would just be GetLogger()?

我知道扩展方法在声明的位置通常是静态的,但是有没有办法在它们正在帮助"的类上显示静态?

I know that normally extension methods are static where they are declared, but is there a way to appear static on the class they are "Helping"?

推荐答案

您可以使用然后您可以这样使用:

string foo = "bar";
ILogger logger = foo.GetLogger();

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

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