C# 由于其保护级别而无法访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1845162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
inaccessible due to its protection level
提问by peter
i am displaying version(WrmVersion) value in a listview ,,but here i given one codition means if version(WrmVersion) is null i am displaying 'None'(ResourcePolicyAvailSystemsLVI.m_nullString="None"),,but now i am getting an error
我在列表视图中显示 version(WrmVersion) 值,但在这里我给出了一个代码意味着如果 version(WrmVersion) 为空我显示“无”(ResourcePolicyAvailSystemsLVI.m_nullString="None"),但现在我得到一个错误
'Ship.Controls.ResourcePolicySystemsControl.ResourcePolicyAvailSystemsLVI.m_nullString' is inaccessible due to its protection level F:\test\Ship\Controls\ResourcePolicySystemsControl.cs 81 70 Ship.Controls
'Ship.Controls.ResourcePolicySystemsControl.ResourcePolicyAvailSystemsLVI.m_nullString' 由于其保护级别 F:\test\Ship\Controls\ResourcePolicySystemsControl.cs 81 70 Ship.Controls 而无法访问
protected override void OnUpdate()
{
string func = "ResourcePolicySystemsLVI.OnUpdate";
try
{
if(Data != null)
{
Text = base.Data.Name;
if(SubItems.Count == 1)
{
if (Data.WrmVersion == null)
{
SubItems.Add(ResourcePolicyAvailSystemsLVI.m_nullString);
}
else
**SubItems.Add(((IResourcePolicy)Data).WrmVersion.ToString());**
SubItems.Add(((IResourcePolicy)Data).ResourcePolicyEnabled.ToString());
SubItems.Add(((IResourcePolicy)Data).ResourcePolicyCurrent.ToString());
//SubItems.Add(((IResourcePolicy)Data).WrmVersion.ToString());
//SubItems.Add(Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Unisys\Single Point Operations Windows Resource Monitor", "CurrentVersion", "0").ToString());
}
else
{
SubItems[1].Text = ((IResourcePolicy)Data).ResourcePolicyEnabled.ToString();
SubItems[2].Text = ((IResourcePolicy)Data).ResourcePolicyCurrent.ToString();
}
}
base.OnUpdate();
回答by FlySwat
You can't add:
你不能添加:
ResourcePolicyAvailSystemsLVI.m_nullString
because the scope of that member is either private or internal.
因为该成员的范围是私有的或内部的。
You need to make it public (or ideally, expose it via a property).
您需要将其公开(或者理想情况下,通过属性公开它)。
回答by Marc Gravell
Well, what isthe protection level of ResourcePolicyAvailSystemsLVI.m_nullString
? And where is your code in relation? It will be inaccessible if, for example
那么,什么是保护级别ResourcePolicyAvailSystemsLVI.m_nullString
?你的代码在哪里?它将无法访问,例如
- it is
private
and you're in an unrelated class - it is
protected
and you're not in a subclass - it is
internal
and you're in a different assembly without [InternalsVisibleTo]
- it is
protected internal
and both of the two above apply
- 是的,
private
而且你在一个不相关的班级 - 它是,
protected
而且你不在一个子类中 - 它是,
internal
而你在一个不同的程序集中没有 [InternalsVisibleTo]
- 它是,
protected internal
并且上面两个都适用
To be honest, it lookslike a field, and fields generallyaren't public
- so it wouldn't amaze me if somebody has changed the accessibility, perhaps adding a public static property to wrap it - or simply changed the name (although that would give a different error). Try looking for ResourcePolicyAvailSystemsLVI.NullString
or similar (in intellisense / object-browser).
老实说,它看起来像一个字段,而字段通常不是public
- 所以如果有人改变了可访问性,也许添加一个公共静态属性来包装它 - 或者只是更改名称(尽管那样会给出不同的错误)。尝试寻找ResourcePolicyAvailSystemsLVI.NullString
或类似(在智能感知/对象浏览器中)。
Re your comment; you have:
回复您的评论;你有:
private static string m_nullString =
Managers.ControlStrings.GetString("ManagedDeviceWizard.None");
so just add:
所以只需添加:
public static string NullString {get {return m_nullString;}}
and change your calling code to use ResourcePolicyAvailSystemsLVI.NullString
.
并更改您的调用代码以使用ResourcePolicyAvailSystemsLVI.NullString
.