Tuesday, July 22, 2014

CLR via C# - CHAPTER 6 Type and Member Basics

 Type and Member Basics
The Different Kinds of Type Members

Friend Assemblies
When an assembly is built, it can indicate other assemblies it considers “friends” by using the InternalsVisibleToattribute defined in the System.Runtime.CompilerServicesnamespace.

When the type of a field, method parameter, or method return using System;
using System.Runtime.CompilerServices; // For InternalsVisibleTo attribute
// This assembly's internal types can be accessed by any code written
// in the following two assemblies (regardless of version or culture):
[assembly:InternalsVisibleTo("Wintellect, PublicKey=12345678...90abcdef")]
[assembly:InternalsVisibleTo("Microsoft, PublicKey=b77a5c56...1934e089")]
internal sealed class SomeInternalType { ... }
internal sealed class AnotherInternalType { ... }

Member Accessibility

The CLR requires that all members of an interface type be public. The C# compiler knows this and forbids the programmer from explicitly specifying accessibility on interface members; the compiler just makes all the members publicfor you.

When a derived type is overriding a member defined in its base type, the C# compiler requires that the original member and the overriding member have the same accessibility. That is, if the member in the base class is protected, the overriding member in the derived class must also be protected. However, this is a C# restriction, not a CLR restriction. When deriving from a base class, the CLR allows a member’s accessibility to become less restrictive but not more restrictive. For example, a class can override a protectedmethod defined in its base class and make the overridden method public(more accessible). However, a class cannot override a protectedmethod defined in its base class and make the overridden method private(less accessible). The reason a class cannot make a base class method more restricted is because a user of the derived class could always cast to the base type and gain access to the base class’s method. If the CLR allowed the derived type’s method to be less accessible, it would be making a claim that was not enforceable.

Static Classes
The compiler enforces many restrictions on a staticclass:
■ The class must be derived directly from System.Objectbecause deriving from any other base class makes no sense because inheritance applies only to objects, and you cannot create an instance of a staticclass.
■ The class must not implement any interfaces because interface methods are callable only when using an instance of a class.
■ The class must define only staticmembers (fields, methods, properties, and events). Any instance members cause the compiler to generate an error.
■ The class cannot be used as a field, method parameter, or local variable because all of these would indicate a variable that refers to an instance, and this is not allowed. If the compiler detects any of these uses, the compiler issues an error.

Defining a class by using the statickeyword causes the C# compiler to make the class both abstractand sealed. Furthermore, the compiler will not emit an instance constructor method into the type.

Partial Classes, Structures, and Interfaces

Components, Polymorphism, and Versioning

How the CLR Calls Virtual Methods, Properties, and Events

Using Type Visibility and Member Accessibility Intelligently

Dealing with Virtual Methods When Versioning Types



































































































































No comments:

Post a Comment