CLSCompliantAttribute 類別


表示程式項目是否符合 Common Language Specification (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
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)]
public sealed class CLSCompliantAttribute : 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 屬性是用來指出特定的程式元素是否符合 Common Language Specification (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 引數以 CLSCompliantAttribute 標記。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 (在 Visual Basic 中Imports)子句和程式碼之前發生。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 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)


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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。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)