【发布时间】:2013-03-14 07:50:24
【问题描述】:
我有一个 C# 类,它使用 using 关键字为一些公共属性定义了一些别名。这会导致“不一致的可访问性”编译器错误,因为别名定义的类型显然不是公共的。如何将别名定义的类型也公开,以便错误消失?
// Alias defined at the top of the source file just below the other "using"
// "using" statements that bring in the needed modules.
using TDynamicStringArray = System.Collections.Generic.List<string>;
// Public property defined with the type alias.
public TDynamicStringArray Strs
{
...
}
这是从编译器收到的错误:
Error 2 Inconsistent accessibility: property type 'TDynamicStringArray' is less accessible than property 'Strs'
我尝试将 public 放在 using 前面,但这不起作用。我查看了其他几个关于“不一致的可访问性”主题的 SO 线程,但没有看到任何处理使用 using 关键字创建的类型别名的任何线程。
我使用别名的原因是因为我正在从另一种语言转换一些旧代码,它简化了转换过程。否则我只会使用没有别名的基础类型。
【问题讨论】:
-
出于兴趣,我可能遗漏了一些东西,但您为什么要使用
TDynamicStringArray,而List<string>也同样易于书写和阅读? -
我同意@AshBurlaczenko,即使它是使用完全限定名称定义的,使用别名似乎也很愚蠢
-
@AshBurlaczenko - 正如我在帖子中所说,我正在转换旧代码,并且该类型与旧代码交织在一起的方式使得使用别名更容易。
-
这是在我的 VS2010 中编译的,您能否更好地解释错误的上下文或提供更多代码?
标签: c# visual-studio types properties