Can I import a static class as a namespace to call its methods without specifying the class name in C#?(我可以将静态类作为命名空间导入以调用其方法,而无需在 C# 中指定类名吗?)
问题描述
我广泛使用了一个特定静态类的成员函数.每次调用它的方法时都指定类名看起来很讨厌......
I make extensive use of member functions of one specific static class. Specifying the class name every time I call it's methods looks nasty...
我可以在不指定类名 C# 的情况下将静态类作为命名空间来调用其方法吗?
Can I import a static class as a namespace to call its methods without specifying the class name C#?
推荐答案
如果你的意思是导入它使得它的方法是全局的,不.
If you mean import it such that it's methods are global, no.
不过,您可能想查看扩展方法.它们是静态方法,当它们的类的命名空间被导入时,它们会在它们的第一个参数的类型上显示为实例方法.在此处查看更多信息:http://msdn.microsoft.com/en-us/library/bb383977.aspx
You might want to look at extension methods though. They are static methods that, when their class's namespace is imported, show up as instance methods on the type of their first argument. See more here: http://msdn.microsoft.com/en-us/library/bb383977.aspx
这篇关于我可以将静态类作为命名空间导入以调用其方法,而无需在 C# 中指定类名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以将静态类作为命名空间导入以调用其方法,而无需在 C# 中指定类名吗?


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01