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)]
[System.Serializable]
public sealed class CLSCompliantAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class CLSCompliantAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)>]
type CLSCompliantAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)>]
[<System.Serializable>]
type CLSCompliantAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CLSCompliantAttribute = class
    inherit Attribute
Public NotInheritable Class CLSCompliantAttribute
Inherits Attribute
繼承
CLSCompliantAttribute
屬性

範例

下列範例會將套用 CLSCompliantAttribute 至整個元件。The following example applies a CLSCompliantAttribute to the entire assembly.

using System;
    [assembly: CLSCompliant(true)]

下列宣告會產生 CLS 符合性警告,因為 UInt32 未在 cls 中指定類型。The following declaration generates a CLS-compliance warning because the type UInt32 is not specified in the CLS.

public int SetValue(UInt32 value);

如果宣告是以 a 標記 CLSCompliantAttribute ,則不會產生任何編譯器警告或錯誤。If the declaration is marked with a CLSCompliantAttribute, no compiler warning or error is generated.

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

備註

CLSCompliantAttribute屬性是用來指出特定程式元素是否符合 Common Language Specification (CLS) ,此專案會定義以 .net 為目標的任何語言都必須支援的功能。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 .NET must support. CLS 合規性主要是針對想要確保以 .NET 為目標的任何語言都可存取其程式庫的程式庫開發人員所關注。CLS compliance is primarily of concern to library developers who want to ensure that their libraries are accessible in any language that targets .NET. 如需詳細資訊,請參閱 語言獨立性和 Language-Independent 元件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 類別的執行個體,該布林值會指出指定的程式項目是否符合 CLS 標準。Initializes an instance of the CLSCompliantAttribute class with a Boolean value indicating whether the indicated program element is CLS-compliant.

屬性

IsCompliant

取得布林值,表示指定程式項目是否符合 CLS 標準。Gets the Boolean value indicating whether the indicated program element is CLS-compliant.

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。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)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (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)

適用於

另請參閱