FeatureSwitchDefinitionAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示指定的公用靜態布爾值只讀屬性對應至名稱所指定的功能參數。
public ref class FeatureSwitchDefinitionAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Property, Inherited=false)]
public sealed class FeatureSwitchDefinitionAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Property, Inherited=false)>]
type FeatureSwitchDefinitionAttribute = class
inherit Attribute
Public NotInheritable Class FeatureSwitchDefinitionAttribute
Inherits Attribute
- 繼承
- 屬性
範例
if (Feature.IsSupported)
Feature.Implementation();
public class Feature
{
[FeatureSwitchDefinition("Feature.IsSupported")]
internal static bool IsSupported => AppContext.TryGetSwitch("Feature.IsSupported", out bool isEnabled) ? isEnabled : true;
internal static Implementation() => ...;
}
當應用程式在專案檔中使用下列功能設定進行修剪時, Feature.IsSupported 會被視為 false,並 Feature.Implementation 移除程式碼。
<ItemGroup>
<RuntimeHostConfigurationOption Include="Feature.IsSupported" Value="false" Trim="true" />
</ItemGroup>
備註
IL 重寫器與編譯器可使用此屬性,將指定屬性的回傳值替換為功能切換值。
功能切換的值只能在應用程式層級指定,且影響整個應用程式。 有時會出現應用程式中某個函式庫需要開啟功能開關,但無法向應用程式作者傳達此訊息的情況。
功能切換會讓單元測試和程式碼共享變得複雜,因為不同應用程式設定可能有不同的功能切換值,而且很難確保所有程式碼路徑都能正常運作。
因此,最好將 API 結構化為能自然進行剪裁且不需功能切換的方式。 功能切換器應該保留給需要修剪,但無法自然改變 API 讓修剪變得不可行的情況。
建構函式
| 名稱 | Description |
|---|---|
| FeatureSwitchDefinitionAttribute(String) |
初始化一個新FeatureSwitchDefinitionAttribute類別的實例 並以指定的功能切換名稱。 |
屬性
| 名稱 | Description |
|---|---|
| SwitchName |
提供指定屬性值的功能開關名稱。 |
| TypeId |
在衍生類別中實作時,取得這個 Attribute的唯一標識碼。 (繼承來源 Attribute) |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
傳回值,這個值表示這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| GetHashCode() |
傳回這個實例的哈希碼。 (繼承來源 Attribute) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsDefaultAttribute() |
在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。 (繼承來源 Attribute) |
| Match(Object) |
在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |