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# 中指定类名吗?
基础教程推荐
- C# 9 新特性——record的相关总结 2023-04-03
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 如果条件可以为空 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 将数据集转换为列表 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
