CLSCompliantAttribute 类


指示程序元素是否符合公共语言规范 (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)]
public sealed class CLSCompliantAttribute : Attribute
type CLSCompliantAttribute = class
    inherit Attribute
Public NotInheritable Class CLSCompliantAttribute
Inherits Attribute


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

using System;  
    [assembly: CLSCompliant(true)]  

下面的声明生成了 CLS 符合性警告,因为未在 CLS 中指定类型 UInt32The 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.

 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. 因此,当应用于参数或返回值的程序元素时,将忽略 CLSCompliantAttributeConsequently, 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 的所有公开的类型必须使用 false 参数标记 CLSCompliantAttributeIf 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# usingImports 在 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 类的实例,该值指示所指示的程序元素是否符合 CLS。Initializes an instance of the CLSCompliantAttribute class with a Boolean value indicating whether the indicated program element is CLS-compliant.



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


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

(继承自 Attribute)



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

(继承自 Attribute)

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

(继承自 Attribute)

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

(继承自 Object)

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

(继承自 Attribute)

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

(继承自 Attribute)

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

(继承自 Object)

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)


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

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

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

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

(继承自 Attribute)

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

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

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

(继承自 Attribute)