C#内部访问说明符,

时间:2017-04-20 12:22:37

标签: c# access-specifier

我创建了一个ConsoleApplication来理解访问说明符。

以下是内部的代码,我可以从大会外部访问此类。

namespace Assembly_1 //This is first assembly.
{       
   public class Base
   {
       //internal class
       internal class B
       {
          public static void fnB()
          {
            Console.WriteLine("fnB");
          }
       }        
   }    
}

namespace Assembly_2 //This is second assembly.
{   
    public class Derived : Assembly_1.Base
    {            
        public class D
        {
            public void fnD()
            {
                B.fnB();//how can I access this class?
            }
        }
    }
}

这就是我正在访问它的地方。

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {                
            Assembly_2.Derived.D d = new Assembly_2.Derived.D();
            d.fnD();
        }
    }
}

我的问题

现在我可以访问B类,它的方法类似于Derived中的fnB()。

一切正常。但是如何?

如何在 Assembly_1 之外访问 B类

2 个答案:

答案 0 :(得分:3)

正如我在评论中所说的那样:

您对namespaceassembly条款感到困惑。 你可以在这里阅读:(Assemblies and Namespace

可以在单个assembly中定义许多名称空间。

如果您想查看并了解internal修饰符, 那么你将不得不创建一个新的类库项目(将编译成一个不同的程序集),在那里定义Base类 并在主控制台应用程序中添加对它的引用。 然后您将看到您无法访问它并且代码将无法编译。

答案 1 :(得分:1)

  

如何访问Assembly_1外的B类?

因为您混淆了名称空间和程序集。程序集是一个或多个名称空间的集合,包含在.dll或.exe文件中。

另请参阅:MSDN: Assemblies in the Common Language RuntimeUnderstanding and Using Assemblies and Namespaces in .NET

您在同一程序集中呼叫 final float originalX = x; for (int i = start, inext; i < measureLimit; i = inext) { TextPaint wp = mWorkPaint; wp.set(mPaint); inext = mMetricAffectingSpanSpanSet.getNextTransition(mStart + i, mStart + limit) - mStart; int mlimit = Math.min(inext, measureLimit); ReplacementSpan replacement = null; for (int j = 0; j < mMetricAffectingSpanSpanSet.numberOfSpans; j++) { // Both intervals [spanStarts..spanEnds] and [mStart + i..mStart + mlimit] are NOT // empty by construction. This special case in getSpans() explains the >= & <= tests if ((mMetricAffectingSpanSpanSet.spanStarts[j] >= mStart + mlimit) || (mMetricAffectingSpanSpanSet.spanEnds[j] <= mStart + i)) continue; MetricAffectingSpan span = mMetricAffectingSpanSpanSet.spans[j]; if (span instanceof ReplacementSpan) { replacement = (ReplacementSpan)span; } else { // We might have a replacement that uses the draw // state, otherwise measure state would suffice. span.updateDrawState(wp); } } if (replacement != null) { x += handleReplacement(replacement, wp, i, mlimit, runIsRtl, c, x, top, y, bottom, fmi, needWidth || mlimit < measureLimit); // I think this line making your issue continue; } for (int j = i, jnext; j < mlimit; j = jnext) { jnext = mCharacterStyleSpanSet.getNextTransition(mStart + j, mStart + inext) - mStart; int offset = Math.min(jnext, mlimit); wp.set(mPaint); for (int k = 0; k < mCharacterStyleSpanSet.numberOfSpans; k++) { // Intentionally using >= and <= as explained above if ((mCharacterStyleSpanSet.spanStarts[k] >= mStart + offset) || (mCharacterStyleSpanSet.spanEnds[k] <= mStart + j)) continue; CharacterStyle span = mCharacterStyleSpanSet.spans[k]; span.updateDrawState(wp); } // Only draw hyphen on last run in line if (jnext < mLen) { wp.setHyphenEdit(0); } x += handleText(wp, j, jnext, i, inext, runIsRtl, c, x, top, y, bottom, fmi, needWidth || jnext < measureLimit, offset); } } Assembly_1 名称空间

由于Assembly_2成员在同一个程序集中可见,因此您可以使用internal中的Assembly_1.B,因为两个名称空间都位于同一个程序集中。