CLSCompliantAttribute CLSCompliantAttribute CLSCompliantAttribute CLSCompliantAttribute Class

定义

指示程序元素是否符合公共语言规范 (CLS)。Indicates whether a program element is compliant with the Common Language Specification (CLS). 此类不能被继承。This class cannot be inherited.

public ref class CLSCompliantAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class CLSCompliantAttribute : Attribute
type CLSCompliantAttribute = class
    inherit Attribute
Public NotInheritable Class CLSCompliantAttribute
Inherits Attribute
继承
CLSCompliantAttributeCLSCompliantAttributeCLSCompliantAttributeCLSCompliantAttribute
属性

示例

下面的示例应用CLSCompliantAttribute到整个程序集。The following example applies a CLSCompliantAttribute to the entire assembly.

using System;  
    [assembly: CLSCompliant(true)]  

下面的声明生成 CLS 遵从性警告,因为类型UInt32CLS 中未指定。The following declaration generates a CLS-compliance warning because the type UInt32 is not specified in the CLS.

public int SetValue(UInt32 value);  

如果声明标记有CLSCompliantAttribute,生成编译器警告或错误。If the declaration is marked with a CLSCompliantAttribute, no compiler warning or error is generated.

[CLSCompliant(false)]  
 public int SetValue(UInt32 value);  

注解

CLSCompliantAttribute特性用于指示特定的程序元素是否符合与公共语言规范 (CLS),用于定义面向.NET Framework 的任何语言都必须支持的功能。The CLSCompliantAttribute attribute is used to indicate whether a particular program element complies with the Common Language Specification (CLS), which defines the features that any language that targets the .NET Framework must support. CLS 遵从性是主要的考虑因素对库的开发人员想要确保其库都可在任何面向.NET Framework 语言中访问。CLS compliance is primarily of concern to library developers who want to ensure that their libraries are accessible in any language that targets the .NET Framework. 有关详细信息,请参阅语言独立性和与语言无关的组件For more information, See Language Independence and Language-Independent Components.

您可以将应用CLSCompliantAttribute以下程序元素的属性: 程序集、 模块、 类、 结构、 枚举、 构造函数、 方法、 属性、 字段、 事件、 接口、 委托、 参数和返回值。You can apply the CLSCompliantAttribute attribute to the following program elements: assembly, module, class, struct, enum, constructor, method, property, field, event, interface, delegate, parameter, and return value. 但是,这一概念的 CLS 遵从性才有意义的程序集、 模块、 类型和成员类型,不包括成员签名的一部分。However, the notion of CLS compliance is only meaningful for assemblies, modules, types, and members of types, not parts of a member signature. 因此,CLSCompliantAttribute时应用于参数或返回值的程序元素,将忽略。Consequently, CLSCompliantAttribute is ignored when applied to parameter or return value program elements.

如果没有CLSCompliantAttribute应用于程序元素,则默认情况下:If no CLSCompliantAttribute is applied to a program element, then by default:

  • 程序集不符合 CLS 規格。The assembly is not CLS-compliant.

  • 仅当其封闭类型或程序集不符合 cls 的类型为符合 CLS 規格。The type is CLS-compliant only if its enclosing type or assembly is CLS-compliant.

  • 仅当类型为符合 cls 的成员是类型的符合 CLS 規格。The member of a type is CLS-compliant only if the type is CLS-compliant.

如果程序集标记为符合 CLS 規格,必须使用标记中不符合 cls 的程序集的任何公开的类型CLSCompliantAttribute使用false参数。If an assembly is marked as CLS-compliant, any publicly exposed type in the assembly that is not CLS-compliant must be marked with CLSCompliantAttribute using a false argument. 同样,如果一个类标记为符合 CLS 規格,则必须单独标记不符合 CLS 規格的所有成员。Similarly, if a class is marked as CLS-compliant, you must individually mark all members that are not CLS-compliant. 不符合标准的所有成员必须都提供相应的符合 cls 的替代项。All non-compliant members must provide corresponding CLS-compliant alternatives.

应用于程序集或模块的属性必须出现在 C# 后using(Imports在 Visual Basic 中) 子句和代码的前面。Attributes that are applied to assemblies or modules must occur after the C# using (Imports in Visual Basic) clauses and before the code.

有关使用属性的详细信息,请参阅属性For more information about using attributes, see Attributes.

备注

当前的 Microsoft Visual Basic 编译器有意不会生成 CLS 遵从性警告,但是,将来的版本的编译器将发出警告。The current Microsoft Visual Basic compiler intentionally does not generate a CLS-compliance warning, however, a future release of the compiler will issue that warning.

构造函数

CLSCompliantAttribute(Boolean) CLSCompliantAttribute(Boolean) CLSCompliantAttribute(Boolean) CLSCompliantAttribute(Boolean)

用布尔值初始化 CLSCompliantAttribute 类的实例,该值指示所指示的程序元素是否符合 CLS。Initializes an instance of the CLSCompliantAttribute class with a Boolean value indicating whether the indicated program element is CLS-compliant.

属性

IsCompliant IsCompliant IsCompliant IsCompliant

获取指示所指示的程序元素是否符合 CLS 的布尔值。Gets the Boolean value indicating whether the indicated program element is CLS-compliant.

TypeId TypeId TypeId TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

适用于

另请参阅