13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Expos<strong>in</strong>g <strong>in</strong>ternal members to selected assemblies201List<strong>in</strong>g 7.11 uses fixed-size buffers for the table of colors. Before fixed-size buffers, wecould still have used the API either with a field for each color table entry or by marshal<strong>in</strong>ga normal array as UnmanagedType.ByValArray. However, this would have createda separate array on the heap <strong>in</strong>stead of keep<strong>in</strong>g the <strong>in</strong>formation all with<strong>in</strong> thestructure. That’s not a problem here, but <strong>in</strong> some high-performance situations it’snice to be able to keep “lumps” of data together. On a different performance note, ifthe buffer is part of a data structure on the managed heap, you have to p<strong>in</strong> it beforeaccess<strong>in</strong>g it. If you do this a lot, it can significantly affect the garbage collector. Stackbasedstructures don’t have this problem, of course.I’m not go<strong>in</strong>g to claim that fixed-size buffers are a hugely important feature <strong>in</strong><strong>C#</strong> 2—at least, they’re not important to most people. I’ve <strong>in</strong>cluded them for completeness,however, and doubtless someone, somewhere will f<strong>in</strong>d them <strong>in</strong>valuable.Our f<strong>in</strong>al feature can barely be called a <strong>C#</strong> 2 language feature at all—but it just aboutcounts, so I’ve <strong>in</strong>cluded it for completeness.7.7 Expos<strong>in</strong>g <strong>in</strong>ternal members to selected assembliesThere are some features that are obviously <strong>in</strong> the language—iterator blocks, for example.There are some features that obviously belong to the runtime, such as JIT compileroptimizations. There are some that clearly sit <strong>in</strong> both camps, like generics. Thislast feature has a toe <strong>in</strong> each but is sufficiently odd that it doesn’t merit a mention <strong>in</strong>either specification. In addition, it uses a term that has different mean<strong>in</strong>gs <strong>in</strong> C++ andVB.NET—add<strong>in</strong>g a third mean<strong>in</strong>g to the mix. To be fair, all the terms are used <strong>in</strong> thecontext of access permissions, but they have different effects.7.7.1 Friend assemblies <strong>in</strong> the simple caseIn .NET 1.1 it was entirely accurate to say that someth<strong>in</strong>g def<strong>in</strong>ed to be <strong>in</strong>ternal(whether a type, a method, a property, a variable, or an event) could only beaccessed with<strong>in</strong> the same assembly <strong>in</strong> which it was declared. 3 In .NET 2.0 that’s stillmostly true, but there’s a new attribute to let you bend the rules slightly: Internals-VisibleToAttribute, usually referred to as just InternalsVisibleTo. (When apply<strong>in</strong>gan attribute whose name ends with Attribute, the <strong>C#</strong> compiler will apply thesuffix automatically.)InternalsVisibleTo can only be applied to an assembly (not a specific member),and you can apply it multiple times to the same assembly. We will call the assemblyconta<strong>in</strong><strong>in</strong>g the attribute the source assembly, although this is entirely unofficial term<strong>in</strong>ology.When you apply the attribute, you have to specify another assembly, known asthe friend assembly. The result is that the friend assembly can see all the <strong>in</strong>ternal membersof the source assembly as if they were public. This may sound alarm<strong>in</strong>g, but it canbe useful, as we’ll see <strong>in</strong> a m<strong>in</strong>ute.List<strong>in</strong>g 7.12 shows this with three classes <strong>in</strong> three different assemblies.3Us<strong>in</strong>g reflection when runn<strong>in</strong>g with suitable permissions doesn’t count.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!