Using-directives facilitate the use of namespaces and types defined in other
namespaces. Using-directives
impact the name resolution process of namespace-or-type-names (§10.8) and
simple-names (§14.5.2), but
unlike declarations, using-directives do not contribute new members to the
underlying declaration spaces of
the compilation units or namespaces within which they are used.
Chapter 16 Namespaces
203
using-directives:
using-directive
using-directives using-directive
using-directive:
using-alias-directive
using-namespace-directive
A using-alias-directive (§16.3.1) introduces an alias for a namespace or
type.
A using-namespace-directive (§16.3.2) imports the type members of a
namespace.
The scope of a using-directive extends over the
namespace-member-declarations of its immediately
containing compilation unit or namespace body. The scope of a
using-directive specifically does not include
its peer using-directives. Thus, peer using-directives do not affect each
other, and the order in which they are
written is insignificant.
16.3.1 Using alias directives
A using-alias-directive introduces an identifier that serves as an alias
for a namespace or type within the
immediately enclosing compilation unit or namespace body.
using-alias-directive:
using identifier = namespace-or-type-name ;
Within member declarations in a compilation unit or namespace body that
contains a using-alias-directive,
the identifier introduced by the using-alias-directive can be used to
reference the given namespace or type.
[Example: For example:
namespace N1.N2
{
class A {}
}
namespace N3
{
using A = N1.N2.A;
class B: A {}
}
Above, within member declarations in the N3 namespace, A is an alias for
N1.N2.A, and thus class N3.B
derives from class N1.N2.A. The same effect can be obtained by creating an
alias R for N1.N2 and then
referencing R.A:
namespace N3
{
using R = N1.N2;
class B: R.A {}
}
end example]
The identifier of a using-alias-directive must be unique within the
declaration space of the compilation unit
or namespace that immediately contains the using-alias-directive. [Example:
For example:
namespace N3
{
class A {}
}
namespace N3
{
using A = N1.N2.A; // Error, A already exists
}
C# LANGUAGE SPECIFICATION
204
Above, N3 already contains a member A, so it is a compile-time error for a
using-alias-directive to use that
identifier. end example] Likewise, it is a compile-time error for two or
more using-alias-directives in the
same compilation unit or namespace body to declare aliases by the same name.
A using-alias-directive makes an alias available within a particular
compilation unit or namespace body, but
it does not contribute any new members to the underlying declaration space.
In other words, a using-aliasdirective
is not transitive, but, rather, affects only the compilation unit or
namespace body in which it occurs.
[Example: In the example
namespace N3
{
using R = N1.N2;
}
namespace N3
{
class B: R.A {} // Error, R unknown
}
the scope of the using-alias-directive that introduces R only extends to
member declarations in the
namespace body in which it is contained, so R is unknown in the second
namespace declaration. However,
placing the using-alias-directive in the containing compilation unit causes
the alias to become available
within both namespace declarations:
using R = N1.N2;
namespace N3
{
class B: R.A {}
}
namespace N3
{
class C: R.A {}
}
end example]
Just like regular members, names introduced by using-alias-directives are
hidden by similarly named
members in nested scopes. [Example: In the example
using R = N1.N2;
namespace N3
{
class R {}
class B: R.A {} // Error, R has no member A
}
the reference to R.A in the declaration of B causes a compile-time error
because R refers to N3.R, not
N1.N2. end example]
The order in which using-alias-directives are written has no significance,
and resolution of the namespaceor-
type-name referenced by a using-alias-directive is not affected by the
using-alias-directive itself or by
other using-directives in the immediately containing compilation unit or
namespace body. In other words,
the namespace-or-type-name of a using-alias-directive is resolved as if the
immediately containing
compilation unit or namespace body had no using-directives. [Example: In
the example
namespace N1.N2 {}
namespace N3
{
using R1 = N1; // OK
using R2 = N1.N2; // OK
using R3 = R1.N2; // Error, R1 unknown
}
Chapter 16 Namespaces
205
the last using-alias-directive results in a compile-time error because it
is not affected by the first using-aliasdirective.
end example]
A using-alias-directive can create an alias for any namespace or type,
including the namespace within which
it appears and any namespace or type nested within that namespace.
Accessing a namespace or type through an alias yields exactly the same
result as accessing that namespace
or type through its declared name. [Example: For example, given
namespace N1.N2
{
class A {}
}
namespace N3
{
using R1 = N1;
using R2 = N1.N2;
class B
{
N1.N2.A a; // refers to N1.N2.A
R1.N2.A b; // refers to N1.N2.A
R2.A c; // refers to N1.N2.A
}
}
the names N1.N2.A, R1.N2.A, and R2.A are equivalent and all refer to the
class whose fully qualified
name is N1.N2.A. end example]
16.3.2 Using namespace directives
A using-namespace-directive imports the types contained in a namespace into
the immediately enclosing
compilation unit or namespace body, enabling the identifier of each type to
be used without qualification.
using-namespace-directive:
using namespace-name ;
Within member declarations in a compilation unit or namespace body that
contains a using-namespacedirective,
the types contained in the given namespace can be referenced directly.
[Example: For example:
namespace N1.N2
{
class A {}
}
namespace N3
{
using N1.N2;
class B: A {}
}
Above, within member declarations in the N3 namespace, the type members of
N1.N2 are directly
available, and thus class N3.B derives from class N1.N2.A. end example]
A using-namespace-directive imports the types contained in the given
namespace, but specifically does not
import nested namespaces. [Example: In the example
namespace N1.N2
{
class A {}
}
namespace N3
{
using N1;
class B: N2.A {} // Error, N2 unknown
}
C# LANGUAGE SPECIFICATION
206
the using-namespace-directive imports the types contained in N1, but not
the namespaces nested in N1. Thus,
the reference to N2.A in the declaration of B results in a compile-time
error because no members named N2
are in scope. end example]
Unlike a using-alias-directive, a using-namespace-directive may import
types whose identifiers are already
defined within the enclosing compilation unit or namespace body. In effect,
names imported by a usingnamespace-
directive are hidden by similarly named members in the enclosing
compilation unit or
namespace body. [Example: For example:
namespace N1.N2
{
class A {}
class B {}
}
namespace N3
{
using N1.N2;
class A {}
}
Here, within member declarations in the N3 namespace, A refers to N3.A
rather than N1.N2.A. end example]
When more than one namespace imported by using-namespace-directives in the
same compilation unit or
namespace body contain types by the same name, references to that name are
considered ambiguous.
[Example: In the example
namespace N1
{
class A {}
}
namespace N2
{
class A {}
}
namespace N3
{
using N1;
using N2;
class B: A {} // Error, A is ambiguous
}
both N1 and N2 contain a member A, and because N3 imports both, referencing
A in N3 is a compile-time
error. end example] In this situation, the conflict can be resolved either
through qualification of references
to A, or by introducing a using-alias-directive that picks a particular A.
[Example: For example:
namespace N3
{
using N1;
using N2;
using A = N1.A;
class B: A {} // A means N1.A
}
end example]
Like a using-alias-directive, a using-namespace-directive does not
contribute any new members to the
underlying declaration space of the compilation unit or namespace, but,
rather, affects only the compilation
unit or namespace body in which it appears.
The namespace-name referenced by a using-namespace-directive is resolved in
the same way as the
namespace-or-type-name referenced by a using-alias-directive. Thus,
using-namespace-directives in the
same compilation unit or namespace body do not affect each other and can be
written in any order.
分享到:
相关推荐
在这篇文章中,我们要讲述如何利用AngularJS的Directive功能来自定义一个表格。 首先,我们要明确什么是AngularJS中的Directive。Directive是AngularJS的最小指令性组件,它可以通过添加属性、元素、注释、或者类来...
drag and drop using handler 5.import { Draggable } from 'draggable-vue-directive' ... export default { directives: { Draggable, }, data() { return { handleId: "handle-id", draggableValue: {...
DIRECTIVE 2011/65/EU OF THE EUROPEAN PARLIAMENT AND OF THE COUNCIL of 8 June 2011 on the restriction of the use of certain hazardous substances in electrical and electronic equipment (recast) ...
Vue.directive(‘my-directive’, { bind: function () {}, inserted: function () {}, update: function () {}, componentUpdated: function () {}, unbind: function () {} }) {{msg}} 更新 ```import Vue ...
它提供了丰富的功能,包括数据绑定、依赖注入、指令等,而"angularjs-router-directive"则聚焦于AngularJS中的路由和指令两个核心概念。 路由在AngularJS中是通过`ngRoute`模块实现的,它允许我们在应用中定义多个...
就我对directive的粗浅理解,它一般用于独立Dom元素的封装,应用场合为控件重用和逻辑模块分离。后者我暂时没接触,但数据交互部分却是一样的。所以举几个前者的例子,以备以后忘记。 directive本身的作用域$scope...
标题 "DIRECTIVE 2014-40-EU OF THE EUROPEAN PARLIAMENT AND OF THE COUNC" 指的是欧洲议会和欧盟理事会于2014年发布的一项指令,通常这类指令旨在规范欧盟成员国在特定领域的法律框架。在IT行业中,这种指令可能...
在AngularJS中,自定义指令通过`@Directive`装饰器声明,其基本结构包括: ```javascript app.directive('directiveName', function() { return { restrict: 'AECM', // A - attribute, E - element, C - class, ...
在这个名为"前端项目-tree-grid-directive.zip"的压缩包中,包含了一个使用AngularJS实现的树网格指令。AngularJS是一款强大的前端JavaScript框架,它通过声明式编程和依赖注入来简化Web应用的开发。下面将详细探讨...
"Directive translation_01.zip" 文件提供了一个实现这种通讯的桥梁,它使得上位机能够有效地与下位机进行交互,执行特定的机器指令。这份资源特别强调其简单易懂的特性,对于初学者或者希望深入理解通讯机制的...
前端项目-angular-pageslide-directive,AngularJS sliding panel for serving additional content from off the page
Laravel开发-laravel-blade-directive-yaml-injector 对你的包裹做什么的简短描述
欧盟垃圾焚烧污染物排放标准DIRECTIVE_2000旨在规范和控制垃圾焚烧过程中产生的有害污染物,以保护环境和公众健康。这一标准依据欧盟成立条约,尤其是第175条,结合第五个环境行动计划,旨在减少氮氧化物(NOx)、...
`directive` 是 Vue 中的一种特殊特性,它允许自定义DOM元素的行为。在本文中,我们将深入理解如何在Vue中实现一个简单的`directive`。 首先,我们来看一下`directive`的基本概念。在Vue中,`directive`是一种特殊...
jsTree-directive, jsTree的Angular 指令 文档 jstree指令jsTree的Angular 指令文档文档使用 bower 安装$ bower i jstree-directive教程使用 jsTree,Angularjs和Expressjs语言构建一个基于We
除了通过完整路径访问命名空间成员,C++还提供了两种简化方式:`using directive`和`using declaration`。 - **using directive**: ``` using namespace <命名空间名称>; ``` 这将导入命名空间中的所有成员,...
在本文中,我们将深入探讨如何使用Angular Directive构建一个Markdown编辑器。Markdown编辑器是一种常见的文本编辑工具,它允许用户以简洁易读的语法输入文本,然后转换为HTML格式,广泛应用于博客、文档编写和笔记...
vuejs-sticky-directive 在纯js和vue上支持顶部和底部粘性。 安装 npm install vue-sticky-directive --save ES2015 // register globally import Sticky from 'vuejs-sticky-directive' Vue . use ( Sticky ) //...
Vue.js是现代前端开发中广泛使用的JavaScript框架之一,它的核心功能之一就是Directive(指令)。Vue中的指令是一种特殊的标记,用于在HTML模板中为DOM元素添加特殊行为。在本文中,将详细介绍Vue.js中的Directive...