简体   繁体   English

如何使C#“使用”别名公开,以避免“不一致的可访问性”错误?

[英]How can I make a C# “using” alias public to avoid “inconsistent accessibility” errors?

I have a C# class that defines a few aliases with the using keyword for some public properties. 我有一个C#类,它为一些公共属性使用using关键字定义了一些别名。 This leads to "inconsistent accessibility" compiler errors since apparently the types defined by the aliases are not public. 这导致“不一致的可访问性”编译器错误,因为显然由别名定义的类型不是公共的。 How can I make the types defined by the aliases public too so the error goes away? 如何将别名定义的类型也设为公用,以便错误消失?

// 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
{
    ...
}

Here is the Error received from the compiler: 这是从编译器收到的错误:

Error   2   Inconsistent accessibility: property type 'TDynamicStringArray' is less accessible than property 'Strs'

I tried putting public in front of using but that doesn't work. 我试着将公共放在使用的前面,但这是行不通的。 I looked at several other SO threads on the topic of "inconsistent accessibility" but did not see any that dealt with type aliases created with the using keyword. 我查看了有关“不一致的可访问性”主题的其他几个SO线程,但是没有看到任何涉及使用using关键字创建的类型别名的线程。

The reason I am using the alias is because I am converting some old code from another language, and it simplifies the conversion process. 我使用别名的原因是因为我正在从另一种语言转换一些旧代码,并且简化了转换过程。 Otherwise I would just use the underlying type without the alias. 否则,我只会使用没有别名的基础类型。

No, you can't change type's accessibility through any other mechanism but changing type itself. 不,您只能通过更改类型本身来通过任何其他机制来更改类型的可访问性。

If you can't modify source for the type your only option is to wrap it in your custom public type. 如果您不能为该类型修改源,则唯一的选择是将其包装在您的自定义公共类型中。

Aliases don't define types; 别名不定义类型。 they just refer to an existing type. 它们只是引用现有类型。 There is no accessiblity associated with an alias. 没有与别名关联的可访问性。

In your example, List<string> is public, and you shouldn't get that error message. 在您的示例中, List<string>是公共的,您不应收到该错误消息。 My guess is that you have a non-public TDynamicStringArray class somewhere, and the alias doesn't get used at all. 我的猜测是您在某个地方有一个非公共的TDynamicStringArray类,并且别名根本没有被使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM