Invocations on boxed instances 


Мы поможем в написании ваших работ!



ЗНАЕТЕ ЛИ ВЫ?

Invocations on boxed instances



A function member implemented in a value-type can be invoked through a boxed instance of that value-type in the following situations:

· When the function member is an override of a method inherited from type object and is invoked through an instance expression of type object.

· When the function member is an implementation of an interface function member and is invoked through an instance expression of an interface-type.

· When the function member is invoked through a delegate.

In these situations, the boxed instance is considered to contain a variable of the value-type, and this variable becomes the variable referenced by this within the function member invocation. In particular, this means that when a function member is invoked on a boxed instance, it is possible for the function member to modify the value contained in the boxed instance.

Primary expressions

Primary expressions include the simplest forms of expressions.

primary-expression:
primary-no-array-creation-expression
array-creation-expression

primary-no-array-creation-expression:
literal
simple-name
parenthesized-expression
member-access
invocation-expression
element-access
this-access
base-access
post-increment-expression
post-decrement-expression
object-creation-expression
delegate-creation-expression
anonymous-object-creation-expression
typeof-expression
checked-expression
unchecked-expression
default-value-expression
anonymous-method-expression

Primary expressions are divided between array-creation-expressions and primary-no-array-creation-expressions. Treating array-creation-expression in this way, rather than listing it along with the other simple expression forms, enables the grammar to disallow potentially confusing code such as

object o = new int[3][1];

which would otherwise be interpreted as

object o = (new int[3])[1];

Literals

A primary-expression that consists of a literal (§2.4.4) is classified as a value.

Simple names

A simple-name consists of an identifier, optionally followed by a type argument list:

simple-name:
identifier type-argument-listopt

A simple-name is either of the form I or of the form I<A1,..., AK>, where I is a single identifier and <A1,..., AK> is an optional type-argument-list. When no type-argument-list is specified, consider K to be zero. The simple-name is evaluated and classified as follows:

· If K is zero and the simple-name appears within a block and if the block’s (or an enclosing block’s) local variable declaration space (§3.3) contains a local variable, parameter or constant with name I, then the simple-name refers to that local variable, parameter or constant and is classified as a variable or value.

· If K is zero and the simple-name appears within the body of a generic method declaration and if that declaration includes a type parameter with name I, then the simple-name refers to that type parameter.

· Otherwise, for each instance type T (§10.3.1), starting with the instance type of the immediately enclosing type declaration and continuing with the instance type of each enclosing class or struct declaration (if any):

o If K is zero and the declaration of T includes a type parameter with name I, then the simple-name refers to that type parameter.

o Otherwise, if a member lookup (§7.4) of I in T with K type arguments produces a match:

· If T is the instance type of the immediately enclosing class or struct type and the lookup identifies one or more methods, the result is a method group with an associated instance expression of this. If a type argument list was specified, it is used in calling a generic method (§7.6.5.1).

· Otherwise, if T is the instance type of the immediately enclosing class or struct type, if the lookup identifies an instance member, and if the reference occurs within the block of an instance constructor, an instance method, or an instance accessor, the result is the same as a member access (§7.6.4) of the form this.I. This can only happen when K is zero.

· Otherwise, the result is the same as a member access (§7.6.4) of the form T.I or T.I<A1,..., AK>. In this case, it is a binding-time error for the simple-name to refer to an instance member.

· Otherwise, for each namespace N, starting with the namespace in which the simple-name occurs, continuing with each enclosing namespace (if any), and ending with the global namespace, the following steps are evaluated until an entity is located:

o If K is zero and I is the name of a namespace in N, then:

· If the location where the simple-name occurs is enclosed by a namespace declaration for N and the namespace declaration contains an extern-alias-directive or using-alias-directive that associates the name I with a namespace or type, then the simple-name is ambiguous and a compile-time error occurs.

· Otherwise, the simple-name refers to the namespace named I in N.

o Otherwise, if N contains an accessible type having name I and K type parameters, then:

· If K is zero and the location where the simple-name occurs is enclosed by a namespace declaration for N and the namespace declaration contains an extern-alias-directive or using-alias-directive that associates the name I with a namespace or type, then the simple-name is ambiguous and a compile-time error occurs.

· Otherwise, the namespace-or-type-name refers to the type constructed with the given type arguments.

o Otherwise, if the location where the simple-name occurs is enclosed by a namespace declaration for N:

· If K is zero and the namespace declaration contains an extern-alias-directive or using-alias-directive that associates the name I with an imported namespace or type, then the simple-name refers to that namespace or type.

· Otherwise, if the namespaces imported by the using-namespace-directives of the namespace declaration contain exactly one type having name I and K type parameters, then the simple-name refers to that type constructed with the given type arguments.

· Otherwise, if the namespaces imported by the using-namespace-directives of the namespace declaration contain more than one type having name I and K type parameters, then the simple-name is ambiguous and an error occurs.

Note that this entire step is exactly parallel to the corresponding step in the processing of a namespace-or-type-name (§3.8).

· Otherwise, the simple-name is undefined and a compile-time error occurs.

Invariant meaning in blocks

For each occurrence of a given identifier as a full simple-name (without a type argument list) in an expression or declarator, within the local variable declaration space (§3.3) immediately enclosing that occurrence, every other occurrence of the same identifier as a full simple-name in an expression or declarator must refer to the same entity. This rule ensures that the meaning of a name is always the same within a given block, switch block, for-, foreach- or using-statement, or anonymous function.

The example

class Test
{
double x;

void F(bool b) {
x = 1.0;
if (b) {
int x;
x = 1;
}
}
}

results in a compile-time error because x refers to different entities within the outer block (the extent of which includes the nested block in the if statement). In contrast, the example

class Test
{
double x;

void F(bool b) {
if (b) {
x = 1.0;
}
else {
int x;
x = 1;
}
}
}

is permitted because the name x is never used in the outer block.

Note that the rule of invariant meaning applies only to simple names. It is perfectly valid for the same identifier to have one meaning as a simple name and another meaning as right operand of a member access (§7.6.4). For example:

struct Point
{
int x, y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}
}

The example above illustrates a common pattern of using the names of fields as parameter names in an instance constructor. In the example, the simple names x and y refer to the parameters, but that does not prevent the member access expressions this.x and this.y from accessing the fields.

Parenthesized expressions

A parenthesized-expression consists of an expression enclosed in parentheses.

parenthesized-expression:
(expression)

A parenthesized-expression is evaluated by evaluating the expression within the parentheses. If the expression within the parentheses denotes a namespace or type, a compile-time error occurs. Otherwise, the result of the parenthesized-expression is the result of the evaluation of the contained expression.

Member access

A member-access consists of a primary-expression, a predefined-type, or a qualified-alias-member, followed by a “.” token, followed by an identifier, optionally followed by a type-argument-list.

member-access:
primary-expression. identifier type-argument-listopt
predefined-type. identifier type-argument-listopt
qualified-alias-member. identifier type-argument-listopt

predefined-type: one of
bool byte char decimal double float int long
object sbyte short string uint ulong ushort

The qualified-alias-member production is defined in §9.7.

A member-access is either of the form E.I or of the form E.I<A1,..., AK>, where E is a primary-expression, I is a single identifier and <A1,..., AK> is an optional type-argument-list. When no type-argument-list is specified, consider K to be zero.

A member-access with a primary-expression of type dynamic is dynamically bound (§7.2.2). In this case the compiler classifies the member access as a property access of type dynamic. The rules below to determine the meaning of the member-access are then applied at run-time, using the run-time type instead of the compile-time type of the primary-expression. If this run-time classification leads to a method group, then the member access must be the primary-expression of an invocation-expression.

The member-access is evaluated and classified as follows:

· If K is zero and E is a namespace and E contains a nested namespace with name I, then the result is that namespace.

· Otherwise, if E is a namespace and E contains an accessible type having name I and K type parameters, then the result is that type constructed with the given type arguments.

· If E is a predefined-type or a primary-expression classified as a type, if E is not a type parameter, and if a member lookup (§7.4) of I in E with K type parameters produces a match, then E.I is evaluated and classified as follows:

o If I identifies a type, then the result is that type constructed with the given type arguments.

o If I identifies one or more methods, then the result is a method group with no associated instance expression. If a type argument list was specified, it is used in calling a generic method (§7.6.5.1).

o If I identifies a static property, then the result is a property access with no associated instance expression.

o If I identifies a static field:

· If the field is readonly and the reference occurs outside the static constructor of the class or struct in which the field is declared, then the result is a value, namely the value of the static field I in E.

· Otherwise, the result is a variable, namely the static field I in E.

o If I identifies a static event:

· If the reference occurs within the class or struct in which the event is declared, and the event was declared without event-accessor-declarations (§10.8), then E.I is processed exactly as if I were a static field.

· Otherwise, the result is an event access with no associated instance expression.

o If I identifies a constant, then the result is a value, namely the value of that constant.

o If I identifies an enumeration member, then the result is a value, namely the value of that enumeration member.

o Otherwise, E.I is an invalid member reference, and a compile-time error occurs.

· If E is a property access, indexer access, variable, or value, the type of which is T, and a member lookup (§7.4) of I in T with K type arguments produces a match, then E.I is evaluated and classified as follows:

o First, if E is a property or indexer access, then the value of the property or indexer access is obtained (§7.1.1) and E is reclassified as a value.

o If I identifies one or more methods, then the result is a method group with an associated instance expression of E. If a type argument list was specified, it is used in calling a generic method (§7.6.5.1).

o If I identifies an instance property, then the result is a property access with an associated instance expression of E.

o If T is a class-type and I identifies an instance field of that class-type:

· If the value of E is null, then a System.NullReferenceException is thrown.

· Otherwise, if the field is readonly and the reference occurs outside an instance constructor of the class in which the field is declared, then the result is a value, namely the value of the field I in the object referenced by E.

· Otherwise, the result is a variable, namely the field I in the object referenced by E.

o If T is a struct-type and I identifies an instance field of that struct-type:

· If E is a value, or if the field is readonly and the reference occurs outside an instance constructor of the struct in which the field is declared, then the result is a value, namely the value of the field I in the struct instance given by E.

· Otherwise, the result is a variable, namely the field I in the struct instance given by E.

o If I identifies an instance event:

· If the reference occurs within the class or struct in which the event is declared, and the event was declared without event-accessor-declarations (§10.8), and the reference does not occur as the left-hand side of a += or -= operator, then E.I is processed exactly as if I was an instance field.

· Otherwise, the result is an event access with an associated instance expression of E.

· Otherwise, an attempt is made to process E.I as an extension method invocation (§7.6.5.2). If this fails, E.I is an invalid member reference, and a binding-time error occurs.



Поделиться:


Последнее изменение этой страницы: 2016-08-10; просмотров: 225; Нарушение авторского права страницы; Мы поможем в написании вашей работы!

infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 3.238.228.237 (0.02 с.)