静态类的扩展方法?
我知道我可以执行以下操作来延长课程。我有一个静态类,我想扩展。我可以怎样做呢?我想写 ClassName.MyFunc()
static public class SomeName
{
static public int HelperFunction(this SomeClass v)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查此代码。
IsEmail() 的第一个参数是扩展类型实例,而不仅仅是类型本身。你永远不可能拥有静态类型的实例。
Check this code..
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.
您无法在 C# 中扩展静态类。扩展方法通过定义在某些类型上显示为实例方法的静态方法来工作。您不能定义扩展静态类的扩展方法。
You can't extend static classes in C#. Extension methods work by defining static methods that appear as instance methods on some type. You can't define an extension method that extends a static class.
您可能希望将静态类转换为单例。那么该类就只有一个实例。您可以对其使用扩展方法,因为它是一个实例。
前提是您可以访问该类的源代码。
You might want to turn your static class into a singleton. Then there will only be one instance of the class. And you can use extension methods on it because it's an instance.
This is provided you have access to the source code of the class.