我可以在方法范围内用 C# 或 VB.NET 创建别名吗?
是否有等同于别名语句的东西,例如:
// C#:
using C = System.Console;
或:
' VB.NET '
Imports C = System.Console
...但是在方法范围内 - 而不是应用于整个文件?
Is there any equivalent to an aliasing statement such as:
// C#:
using C = System.Console;
or:
' VB.NET '
Imports C = System.Console
...but within method scope -- rather than applying to an entire file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然这可能有点矫枉过正,但您可以创建一个分部类,并仅将您希望别名应用到的函数与别名一起放置在它们自己的文件中。
在主类文件中:
在其他文件中:
While this might be overkill, you could create a partial class and place only the functions you'd like the alias to apply to in their own file with the alias.
In the main class file:
In the other file:
使用对象引用将是合乎逻辑的方式。使用静态类设置了一些障碍。像这样解决:
或者 VB.NET With 语句:
Using an object reference would be the logical way. You are putting up a bit of an obstacle by using a static class. Worked around like this:
Or the VB.NET With statement:
请参阅此处和此处了解更多详细信息。
例子:
See here and here for more details.
Example: