可能重复:
What is the difference between 'protected' and 'protected internal'?
What is the difference between Public, Private, Protected, and Nothing?困惑:内部,保护,受保护的内部
代码是如下所述:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testanotherlib
{
public class A
{
internal void InternalDisplay()
{
Console.WriteLine("Internal Display Method.");
}
protected void ProtectedDisplay()
{
Console.WriteLine("Protected Display Method.");
}
protected internal void ProtectedInternalDisplay()
{
Console.WriteLine("ProtectedInternal Display Method.");
}
public void PublicDisplay()
{
Console.WriteLine("Public Display Method.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testanotherlib
{
public class B : A
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using testanotherlib;
namespace testlib
{
public class C:A
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using testlib;
using testanotherlib;
namespace testapp
{
class Program
{
static void Main(string[] args)
{
B objB = new B();
C objC = new C();
}
}
}
我想了解In之间的区别内部,受保护和受保护的内部。为此,我使用上面的代码创建了一个示例。
在类库项目testanotherlib我有类A & B类。在类库项目testlib我有类C.程序类是在一个单独的控制台应用程序。在Program类的主要方法内部,我为B类(objB)和C类(objC)创建了对象。对于objB和objC,只能使用类A的公共方法。我预计B班的所有方法都可以访问。请帮助我理解这一点。如果您需要关于该项目的任何其他信息,请随时询问我。
问候, Priyank
你在哪里期待能够获得A类的所有方法,具有A级的参考?你的代码从来没有试图*使用*成员,这使得很难谈论... – 2012-03-10 14:11:13
@JonSkeet:我期待能够访问所有的方法,如果类A,参考objB。 – 2012-03-10 14:14:34
@PriyankThakkar:从'testApp'? *为什么*你期待那样?例如'testApp'中的代码与'A'不在同一个程序集中,因此任何内部成员都不可见。 – 2012-03-10 14:16:36