8 Visibility, Accessibility, and Hiding


Partition I, section 8.5.3 specifies visibility and accessibility. In addition to these attributes, the metadata stores information about method name hiding. Hiding controls which method names inherited from a base type are available for compile-time name binding.

ANNOTATION

Most programming languages do not distinguish between visibilitiy and accessibility, hence it is a confusing concept. Visibility concerns only top-level types it determines whether or not they are visible outside their assembly or module. Those are the only two possibilities that make any sense for a top-level type, because generally it is always visible inside its own assembly and its subclasses can see it. The accessibility modes apply to nested types, fields, properties, and events, and determine who has access to them everyone, no one, another assembly, or subclasses.

Hiding is a much more complicated issue. It is supported by the VES for the use of compilers but has no impact on the VES itself. Languages use it at compile time to determine whether a parent implementation can be seen in a subclass. However, languages have chosen two different approaches, and it is important for cross-language interoperability to know which model a language uses.

For example, suppose a parent class has a method F(int), and a subclass with a method F(string). For languages like C++, which hide by name, the parent, F(int) would not be visible, because it has the same method name, F, as the child. However, in languages like Java and C#, which hide by name-and-signature, both would be visible, because the signatures do not match. If there were an F(int) in the subclass, it would supersede the parent F, because both name and signature match.

For hiding, compilers are supposed to follow the rules of the language from which they get the module, even if their own rules differ.


8.1 Visibility of Top-Level Types and Accessibility of Nested Types

Visibility is attached only to top-level types, and there are only two possibilities: visible to types within the same assembly, or visible to types regardless of assembly. For nested types (i.e., types that are members of another type), the nested type has an accessibility that further refines the set of methods that can reference the type. A nested type may have any of the seven accessibility modes (see Partition I, section 8.5.3.2), but has no direct visibility attribute of its own, using the visibility of its enclosing type instead.

Because the visibility of a top-level type controls the visibility of the names of all of its members, a nested type cannot be more visible than the type in which it is nested. That is, if the enclosing type is visible only within an assembly, then a nested type with public accessibility is still only available within the assembly. By contrast, a nested type that has assembly accessibility is restricted to use within the assembly even if the enclosing type is visible outside the assembly.

To make the encoding of all types consistent and compact, the visibility of a top-level type and the accessibility of a nested type are encoded using the same mechanism in the logical model of Partition II, section 22.1.14.

8.2 Accessibility

Accessibility is encoded directly in the metadata. See, for example, Partition II, section 21.24.

ANNOTATION

The available accessibility modes are compiler-controlled, private, family, assembly, family-and-assembly, family-or-assembly, and public. For much more information on these, see Partition I, section 8.5.3.2.


8.3 Hiding

Hiding is a compile-time concept that applies to individual methods of a type. The CTS specifies two mechanisms for hiding, specified by a single bit:

  • hide-by-name, meaning that the introduction of a name in a given type hides all inherited members of the same kind (method or field) with the same name.

  • hide-by-name-and-sig, meaning that the introduction of a name in a given type hides any inherited member of the same kind but with precisely the same type (for fields) or signature (for methods, properties, and events).

There is no runtime support for hiding. A conforming implementation of the CLI treats all references as though the names were marked hide-by-name-and-sig. Compilers that desire the effect of hide-by-name can do so by marking method definitions with the newslot attribute (see Partition II, section 14.4.2.3) and correctly choosing the type used to resolve a method reference (see Partition II, section 14.1.3).



The Common Language Infrastructure Annotated Standard (Microsoft. NET Development Series)
The Common Language Infrastructure Annotated Standard (Microsoft. NET Development Series)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 121

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net