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# 中指定类名吗?
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
