C#:如何创建“别名”上课用
我什至不知道它是否被称为别名,但无论如何让我继续。
您知道 C# 中的 System.String 类型如何用“字符串”进行“别名”吗?在 Visual Studio 中,“字符串”采用小写和蓝色文本。我想对我们的一些冗长的类名执行此操作。有些都类似于“RelocatedPlantProductReleaseItem”。我无法更改它的名称,但是这么长的名称确实使代码变得冗长。我希望能够使用“Rppr”来代替。因此,而不是这样:
Re locatedPlantProductReleaseItem Item = new Re locatedPlantProductReleaseItem();
我会这样:
Rppr Item = new Rppr();
我知道我可以创建一个名为的类Rppr 并让它继承自 Re locatedPlantProductReleaseItem,但这似乎很棘手。
另外,我上面给出的场景完全是虚构的。我只是用它来演示我想要实现的基本功能。
另外,当“string”代表 System.String 时,技术上它被称为什么?
我希望这成为全球性的事情。我不想在每个代码文件的顶部指定它。我想在一处指定它,并且它在整个应用程序中的任何地方都可以工作,甚至在引用其程序集的其他程序集中也是如此。
I don't even know if it's called an alias, but let me continue anyway.
You know how the System.String type in C# is sorta "aliased" with "string"? In Visual Studio, "string" is in lowercase and blue text. I'd like to do this to some of our lengthy class names. Some are all like "RelocatedPlantProductReleaseItem". I can't change the name of it, but such a long name really makes the code long-winded. I'd like to be able to use "Rppr" instead. So instead of this:
RelocatedPlantProductReleaseItem Item = new RelocatedPlantProductReleaseItem();
I'd go like:
Rppr Item = new Rppr();
I know that I can create a class called Rppr and have it inherit from RelocatedPlantProductReleaseItem, but that seems hacky.
Also, the scenario I gave above is totally fictitious. I just used it to demonstrate the essential functionality I am trying to achieve.
Also, what is it technically called when "string" represents System.String?
I want this to be a global thing. I don't want to have to specify it at the top of every code file. I want to specify it in one place and it work everywhere in the entire application, even in other assemblies that reference its assembly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 using 指令
EDIT Op 澄清来请求全局解决方案。
C# 中没有等效的
typedef
样式可以创建类型名称的全局替换。您需要在每个想要使用此缩写或使用名称的长版本的文件中使用 using 指令。Use a using directive
EDIT Op clarified to ask for a global solution.
There is no
typedef
style equivalent in C# which can create a global replacement of a type name. You'll need to use a using directive in every file you want to have this abbreviation or go for the long version of the name.string
是 C# 语言提供的一种便利方式,用于引用实际的 CLR 类型System.String
。您可以使用
using
指令获得类似的结果。在文件顶部您想要缩短名称的位置,添加以下行:然后您可以说
ShortName myVar = new ShortName();
请注意,您必须在每个文件的基础上执行此操作。您无法通过单个声明获得与
string
相同的覆盖范围。string
is a convenience offered by the C# language to refer to the actual CLR typeSystem.String
.You can achieve similar results with a
using
directive. At the top of the file where you'd like the name shortened, add this line:Then you can say
ShortName myVar = new ShortName();
Note that you have to do this on a per file basis. You can't get the same coverage as
string
with a single declaration.尝试
参考
Try
Reference
你可以这样做:
You can do this :
从 C#10 开始,现在可以使用
全局
修饰符。可以将其添加到编译中的任何文件,然后应用于所有其他文件,但必须添加在任何没有
global
修饰符的using
指令之前。As of C#10, it is now possible to do this globally by using the
global
modifier.This can be added to any file in the compilation and then applies to all other files, but must be added before any
using
directives that do not have theglobal
modifier.您可以使用以下方法创建别名:
该别名在您声明它的命名空间中可见。
You create an alias using:
That alias is visible in the namespace where you declared it.
您可以在文件顶部添加
using
指令:但它(仅)适用于每个文件,不适用于项目。
You can add a
using
directive at the top of your file:But it (only) works per file, not for a Project.
尝试将以下内容与您的其他用法一起添加
try to add the following with your other usings