共用方式為


在中繼語言 (IL) 輸出中產生旗標或選項的自定義屬性

您可以將這些屬性新增至程序代碼,讓編譯程式發出指定的中繼語言 (IL) 修飾詞。 這些屬性會指示編譯程式在輸出中包含對應的 IL 修飾詞。

屬性 修飾語 評論
System.Runtime.InteropServices.ComImportAttribute import
System.Runtime.InteropServices.DllImportAttribute pinvokeimpl 您可以新增建構函式中列出的選項。
System.Runtime.InteropServices.FieldOffsetAttribute .field 這會設定記憶體佈局的欄位偏移量。
MarshalAsAttribute marshal 您可以設定建構函式中列出的選項。
System.Runtime.CompilerServices.MethodImplAttribute flag 建構函式自變數會指定特定的具名旗標,例如 aggressiveinliningforwardref。 這些旗標也會為 native 字段指定 managedoptilSystem.Runtime.CompilerServices.MethodCodeType 修飾詞。
System.NonSerializedAttribute notserialized
System.Runtime.InteropServices.OptionalAttribute [opt]
System.Runtime.InteropServices.PreserveSigAttribute preservesig
System.SerializableAttribute serializable
System.Runtime.InteropServices.StructLayoutAttribute autosequentialexplicit 您可以使用參數來設定版面設定選項。
System.Runtime.CompilerServices.IndexerNameAttribute 您會將這個屬性新增至索引器,以設定不同的方法名稱。 根據預設,索引器會編譯為名為 Item的屬性。 您可以使用這個屬性來指定不同的名稱。

其中有些自定義屬性是使用其他 C# 語法來套用,而不是將 屬性新增至您的原始程式碼。

屬性 評論
System.Runtime.InteropServices.DefaultParameterValueAttribute 指定 參數的預設值。 使用 預設參數語法
System.Runtime.InteropServices.InAttribute 指定 IL [in] 修飾詞。 使用 inref readonly 修飾詞。
System.Runtime.InteropServices.OutAttribute 指定 IL [out] 修飾詞。 使用 out 修飾詞。
System.Runtime.CompilerServices.SpecialNameAttribute 指定 IL specialname 修飾詞。 編譯程式會自動針對需要這個修飾詞的方法新增此修飾詞。
System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute delegate* 功能需要此屬性。 編譯程式會將它新增至任何需要使用它的 delegate*。 不過,當該方法指派給函式指標時,您必須將這個屬性新增至任何方法宣告。

C# 來源中通常不允許下列屬性。 這裡列出它們以協助使用反射的程式庫作者,並確保您不會建立具有相同完整名稱的自訂屬性。

屬性 評論
System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute 防止舊版編譯程式使用其無法安全解析的元資料。
System.Runtime.CompilerServices.DecimalConstantAttribute 編碼 const decimal 欄位。 執行時間環境不支援將 decimal 值作為常數值。
System.Reflection.DefaultMemberAttribute 使用 System.Runtime.CompilerServices.IndexerNameAttribute編碼索引器。 當其名稱與 Item不同時,此屬性會記下預設索引器。 來源中允許此屬性。
System.Runtime.CompilerServices.DynamicAttribute 編碼簽章中的類型是否為 dynamic (與 object)。
System.Runtime.CompilerServices.ExtensionAttribute 此屬性會記下擴充方法。 編譯程式也會將這個屬性放在包含類別上。
System.Runtime.CompilerServices.FixedBufferAttribute 這個屬性會指定 fixed 結構欄位。
System.Runtime.CompilerServices.IsByRefLikeAttribute 這個屬性會指定 ref 結構。
System.Runtime.CompilerServices.IsReadOnlyAttribute 這個屬性表示參數具有 in 修飾詞。 它會區分 in 參數與 readonly ref[In] ref
System.Runtime.CompilerServices.RequiresLocationAttribute 這個屬性表示參數具有 readonly ref 修飾詞。 它會區分 readonly refin[In] ref
System.Runtime.CompilerServices.IsUnmanagedAttribute 這個屬性會指定類型參數上的 unmanaged 條件約束。
System.Runtime.CompilerServices.NullableAttributeSystem.Runtime.CompilerServices.NullableContextAttributeSystem.Runtime.CompilerServices.NullablePublicOnlyAttribute 這些屬性會在原始碼中編碼可為 Null 的註解。
System.ParamArrayAttribute 此屬性會將陣列參數上的 params 修飾詞編碼。
System.Runtime.CompilerServices.ParamCollectionAttribute 此屬性會將在非陣列參數上的 params 修飾詞編碼。
System.Runtime.CompilerServices.RefSafetyRulesAttribute 這個屬性會指定所需的 C# 版本,以便了解元件中的 ref 安全批注。 C# 隨著新功能的加入,引用安全規則也隨之演變。
System.Runtime.CompilerServices.RequiredMemberAttribute 這個屬性表示 required 修飾詞已放在成員宣告上。 這是 必要成員 語言功能的編碼方式。
System.Runtime.CompilerServices.TupleElementNamesAttribute 此屬性編碼簽章中使用的元組元素名稱。

此外,編譯程式可以針對內部使用的其他屬性產生宣告。 編譯程式會在 System.Runtime.CompilerServices 命名空間中產生這些屬性,以供自己使用。 有些不在 .NET 運行時間連結庫中。 相反地,編譯器會在需要屬性的任何組件中產生 internal 型別宣告的定義。