C#public类型的别名? [英] C# public type alias?

查看:168
本文介绍了C#public类型的别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的就是写一些类在C#中充当包装类的类在另一个名称空间中的新的命名空间。有时候不需要包装类,但我还是想在新的命名空间中一个对应的类。我希望这个类的精确副本。有没有办法通过引用另一个类的定义来定义类的新的命名空间?换句话说,我希望有一个别名。

What I want to do is write some classes in C# in a new namespace that act as wrapper classes for classes in another namespace. Sometimes a wrapper class is not needed but I still want a corresponding class in the new namespace. And I want an exact copy of the class. Is there a way to define the class in the new namespace by referring to the definition of another class? In other words I want an alias.

要澄清我的意思是,如果现有的命名空间被命名为Namespace1和新的命名空间被命名为Namespace2,用code这样的Namespace2:

To clarify what I mean, if the existing namespace is named "Namespace1" and the new namespace is named "Namespace2", using code like this in Namespace2:

using Class1 = Namespace1.Class1;

是行不通的,因为 Namespace2.Class1 也就不存在了。 1级只会别名私来Namespace2而不是公开,以Namespace2。如果我可以使用 Namepsace2.Class1 从命名空间外面,如果仍然参照 Namespace1.Class1 ,然后这将是我想要的。

would not work because Namespace2.Class1 would not exist. Class1 would only be aliased "private" to Namespace2 and not "public" to Namespace2. If I could use Namepsace2.Class1 from outside the namespace, and if that would still refer to Namespace1.Class1, then that would be what I want.

我想有可能是一种方法来达到这个目的的属性或反射可能。如果有一些pre处理器指令或宏,可以复制code,将工作过,但很明显,C#没有这样的事情。

I figured there might be a way to accomplish this with attributes or reflection maybe. If there were some pre-processor directives or macros that could copy code that would work too, but obviously C# doesn't have anything like that.

推荐答案

这听起来像你需要一个类定义映射到另一个。

It sounds like you need to map one class definition to another.

这可以通过手动工具来完成(通过大量的样板code)或自动如 AutoMapper

This can be done manually (through lots of boilerplate code) or automatically through a tool like AutoMapper.

这篇关于C#public类型的别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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