C#:如何创建“别名”;上课 [英] C#: How to create "aliases" for classes

查看:78
本文介绍了C#:如何创建“别名”;上课的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什至不知道它是否被称为别名,但无论如何让我继续。

I don't even know if it's called an alias, but let me continue anyway.

您知道C#中的System.String类型是如何别名的与字符串?在Visual Studio中,字符串以小写和蓝色文本表示。我想对一些冗长的班级名称进行此操作。有些都像 RelocatedPlantProductReleaseItem。我无法更改其名称,但是这么长的名称确实会使代码冗长。我希望可以改用 Rppr。所以代替这个:

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();

我会这样:

Rppr Item = new Rppr();

我知道我可以创建一个名为Rppr的类,并将其从RelocatedPlantProductReleaseItem继承,但这似乎很麻烦。

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.

此外,当字符串表示System.String时,它在技术上叫什么?

Also, what is it technically called when "string" represents System.String?

更新:谢谢大家的回答,伙计们,但是我想我应该指定我希望这是一个全球性的事情。我不想在每个代码文件的顶部都指定它。我想在一个地方指定它,并且它可以在整个应用程序中的任何地方使用,甚至在引用其程序集的其他程序集中也可以。

Update: Thanks for all the answers, guys, but I guess I should have specified that 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.

推荐答案

使用using指令

using Rppr = Namespace.To.RelocatedPlantProductReleaseItem;

编辑明确要求全球解决方案。

EDIT Op clarified to ask for a global solution.

C#中没有 typedef 样式等效项,它可以创建类型名称的全局替换。您需要在每个要使用此缩写的文件中使用using指令,或使用长名称。

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.

这篇关于C#:如何创建“别名”;上课的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆