在中繼語言 (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 |
建構函式自變數會指定特定的具名旗標,例如 aggressiveinlining 或 forwardref 。 這些旗標也會為 native 字段指定 managed 、optil 或 System.Runtime.CompilerServices.MethodCodeType 修飾詞。 |
System.NonSerializedAttribute | notserialized |
|
System.Runtime.InteropServices.OptionalAttribute | [opt] |
|
System.Runtime.InteropServices.PreserveSigAttribute | preservesig |
|
System.SerializableAttribute | serializable |
|
System.Runtime.InteropServices.StructLayoutAttribute |
auto 、sequential 或 explicit |
您可以使用參數來設定版面設定選項。 |
System.Runtime.CompilerServices.IndexerNameAttribute | 您會將這個屬性新增至索引器,以設定不同的方法名稱。 根據預設,索引器會編譯為名為 Item 的屬性。 您可以使用這個屬性來指定不同的名稱。 |
其中有些自定義屬性是使用其他 C# 語法來套用,而不是將 屬性新增至您的原始程式碼。
屬性 | 評論 |
---|---|
System.Runtime.InteropServices.DefaultParameterValueAttribute | 指定 參數的預設值。 使用 預設參數語法。 |
System.Runtime.InteropServices.InAttribute | 指定 IL [in] 修飾詞。 使用 in 或 ref readonly 修飾詞。 |
System.Runtime.InteropServices.OutAttribute | 指定 IL [out] 修飾詞。 使用 out 修飾詞。 |
System.Runtime.CompilerServices.SpecialNameAttribute | 指定 IL specialname 修飾詞。 編譯程式會自動針對需要這個修飾詞的方法新增此修飾詞。 |
System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute |
delegate* 功能需要此屬性。 編譯程式會將它新增至任何需要使用它的 delegate* 。 不過,當該方法指派給函式指標時,您必須將這個屬性新增至任何方法宣告。 |
C# 來源中通常不允許下列屬性。 這裡列出它們以協助使用反射的程式庫作者,並確保您不會建立具有相同完整名稱的自訂屬性。
此外,編譯程式可以針對內部使用的其他屬性產生宣告。 編譯程式會在 System.Runtime.CompilerServices 命名空間中產生這些屬性,以供自己使用。 有些不在 .NET 運行時間連結庫中。 相反地,編譯器會在需要屬性的任何組件中產生 internal
型別宣告的定義。