GenericEnumConverter 類別

定義

在字串和列舉型別之間轉換。

public ref class GenericEnumConverter sealed : System::Configuration::ConfigurationConverterBase
public sealed class GenericEnumConverter : System.Configuration.ConfigurationConverterBase
type GenericEnumConverter = class
    inherit ConfigurationConverterBase
Public NotInheritable Class GenericEnumConverter
Inherits ConfigurationConverterBase
繼承

範例

下列程式代碼範例示範如何存取使用 型別的 GenericEnumConverter 自定義區段屬性。

using System;
using System.IO;
using System.ComponentModel;
using System.Configuration;

namespace Samples.AspNet
{
  
    public sealed class UsingGenericEnumConverter
    {
        public static void GetPermission()
        {
            try
            {
                CustomSection section =
                    ConfigurationManager.GetSection("CustomSection")
                    as CustomSection;
                
                Console.WriteLine("Default Permission: {0}", 
                    section.Permission.ToString());
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public static void SetPermission()
        {
            try
            {
                System.Configuration.Configuration config =
                  ConfigurationManager.OpenExeConfiguration(
                  ConfigurationUserLevel.None);

                CustomSection section =
                    config.Sections.Get("CustomSection")
                    as CustomSection;

                section.Permission = 
                    CustomSection.Permissions.FullControl;

                section.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Full); 
                config.Save();

                Console.WriteLine("Current Protection: {0}",
                    section.Permission.ToString());
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
Imports System.IO
Imports System.ComponentModel
Imports System.Configuration




NotInheritable Public Class UsingGenericEnumConverter
    
    Public Shared Sub GetPermission() 
        Try
            Dim section As CustomSection = _
            ConfigurationManager.GetSection("CustomSection")
            Console.WriteLine("Default Permission: {0}", _
            section.Permission.ToString())
        Catch e As System.Exception
            Console.WriteLine(e.Message)
        End Try
    
    End Sub
    
    
    Public Shared Sub SetPermission() 
        Try
            Dim config _
            As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)
            
            Dim section As CustomSection = _
            config.Sections.Get("CustomSection")
            
            section.Permission = _
            CustomSection.Permissions.FullControl
            
            section.SectionInformation.ForceSave = True
            config.Save(ConfigurationSaveMode.Full)
            config.Save()
            
            Console.WriteLine( _
            "Current Protection: {0}", _
            section.Permission.ToString())
        Catch e As System.Exception
            Console.WriteLine(e.Message)
        End Try
    
    End Sub
End Class

下列程式代碼範例示範如何定義在上一個範例中存取的自定義列舉值屬性。 請注意,不需要指定 GenericEnumConverter 類型;系統會隱含地叫用它。


public enum Permissions
{
    FullControl         = 0,
    Modify              = 1,
    ReadExecute         = 2,
    Read                = 3,
    Write               = 4,
    SpecialPermissions  = 5
}

[ConfigurationProperty("permission", DefaultValue = Permissions.Read)]
public Permissions Permission
{
    get
    {
        return (Permissions)this["permission"];
    }

    set
    {
        this["permission"] = value;
    }
}

Public Enum Permissions
    FullControl = 0
    Modify = 1
    ReadExecute = 2
    Read = 3
    Write = 4
    SpecialPermissions = 5
End Enum 'Permissions


<ConfigurationProperty("permission", _
DefaultValue:=Permissions.Read)> _
Public Property Permission() As Permissions
    Get
        Return CType(Me("permission"), Permissions)
    End Get

    Set(ByVal value As Permissions)
        Me("permission") = Value
    End Set
End Property

下列範例是上一個範例所使用的組態摘錄。

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  

  <configSections>  
    <section name="CustomSection"   
      type="Samples.AspNet.CustomSection,   
      ConfigurationConverters,   
      Version=1.0.0.0, Culture=neutral,   
      PublicKeyToken=null"   
      allowDefinition="Everywhere"   
      allowExeDefinition="MachineToApplication"   
      restartOnExternalChanges="true" />  
  </configSections>  

  <CustomSection fileName="default.txt" maxIdleTime="90" timeDelay="infinite" cdStr="str0, str1" permission="Read" maxUsers="Infinite"/>  

</configuration>  

備註

此類型就像所有其他組態轉換子類型一樣,會將組態檔中找到的字串轉換成相關強型別屬性。

特別是, GenericEnumConverter 物件會將 String 指派給組態屬性的值轉換為值, Enum 反之亦然。

建構函式

GenericEnumConverter(Type)

初始化 GenericEnumConverter 類別的新執行個體。

方法

CanConvertFrom(ITypeDescriptorContext, Type)

判斷是否允許轉換。

(繼承來源 ConfigurationConverterBase)
CanConvertFrom(Type)

傳回值,指出這個轉換子是否可將指定之型別的物件轉換為這個轉換子的型別。

(繼承來源 TypeConverter)
CanConvertTo(ITypeDescriptorContext, Type)

判斷是否允許轉換。

(繼承來源 ConfigurationConverterBase)
CanConvertTo(Type)

傳回值,指出這個轉換子是否可以將物件轉換成指定的型別。

(繼承來源 TypeConverter)
ConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

String 轉換成 Enum 型別。

ConvertFrom(Object)

會將指定值轉換為這個轉換子的型別。

(繼承來源 TypeConverter)
ConvertFromInvariantString(ITypeDescriptorContext, String)

會使用不區分的文化特性和指定的內容,將指定的字串轉換為這個轉換子的型別。

(繼承來源 TypeConverter)
ConvertFromInvariantString(String)

會使用不因文化特性而異,將指定字串轉換為這個轉換子的型別。

(繼承來源 TypeConverter)
ConvertFromString(ITypeDescriptorContext, CultureInfo, String)

會使用指定內容和文化特性資訊,將指定文字轉換為物件。

(繼承來源 TypeConverter)
ConvertFromString(ITypeDescriptorContext, String)

會使用指定的內容,將指定文字轉換為物件。

(繼承來源 TypeConverter)
ConvertFromString(String)

會將指定的文字轉換成物件。

(繼承來源 TypeConverter)
ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

Enum 型別轉換成 String 值。

ConvertTo(Object, Type)

會使用引數,將指定值物件轉換成指定型別。

(繼承來源 TypeConverter)
ConvertToInvariantString(ITypeDescriptorContext, Object)

會使用指定的內容,將指定值轉換成不因文化特性而異的字串表示。

(繼承來源 TypeConverter)
ConvertToInvariantString(Object)

會將指定值轉換成不因文化特性而異的字串表示。

(繼承來源 TypeConverter)
ConvertToString(ITypeDescriptorContext, CultureInfo, Object)

會使用指定的內容和文化特性資訊,將指定值轉換為字串表示。

(繼承來源 TypeConverter)
ConvertToString(ITypeDescriptorContext, Object)

會使用指定的內容,將指定值轉換為字串表示。

(繼承來源 TypeConverter)
ConvertToString(Object)

會將指定值轉換成字串表示。

(繼承來源 TypeConverter)
CreateInstance(IDictionary)

重新建立 Object,需為物件提供一組屬性值。

(繼承來源 TypeConverter)
CreateInstance(ITypeDescriptorContext, IDictionary)

使用指定的內容,建立與這個 TypeConverter 相關聯之類型的執行個體,並為物件提供一組屬性值。

(繼承來源 TypeConverter)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetConvertFromException(Object)

傳回要在無法執行轉換時擲回的例外狀況。

(繼承來源 TypeConverter)
GetConvertToException(Object, Type)

傳回要在無法執行轉換時擲回的例外狀況。

(繼承來源 TypeConverter)
GetCreateInstanceSupported()

傳回值,指出在這個物件上變更值時是否需要呼叫 CreateInstance(IDictionary) 方法來建立新值。

(繼承來源 TypeConverter)
GetCreateInstanceSupported(ITypeDescriptorContext)

傳回在這個物件上變更值是否需要呼叫 CreateInstance(IDictionary) 來建立新的值,並使用指定的內容。

(繼承來源 TypeConverter)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetProperties(ITypeDescriptorContext, Object)

會使用指定的內容,傳回由值參數所指定之陣列型別的屬性集合。

(繼承來源 TypeConverter)
GetProperties(ITypeDescriptorContext, Object, Attribute[])

使用指定的內容和屬性 (attribute),傳回由該值參數所指定之陣列類型的屬性 (property) 集合。

(繼承來源 TypeConverter)
GetProperties(Object)

傳回由值參數所指定的陣列型別的屬性集合。

(繼承來源 TypeConverter)
GetPropertiesSupported()

傳回值,指出這個物件是否支援屬性。

(繼承來源 TypeConverter)
GetPropertiesSupported(ITypeDescriptorContext)

使用指定的內容,傳回這個物件是否支援屬性。

(繼承來源 TypeConverter)
GetStandardValues()

從為這個型別轉換子所設計的資料型別預設內容中,會傳回標準值的集合。

(繼承來源 TypeConverter)
GetStandardValues(ITypeDescriptorContext)

提供格式內容時,傳回此類型轉換子所針對資料類型的標準值集合。

(繼承來源 TypeConverter)
GetStandardValuesExclusive()

傳回值,指出從 GetStandardValues() 所傳回的標準值集合是否為獨佔清單。

(繼承來源 TypeConverter)
GetStandardValuesExclusive(ITypeDescriptorContext)

使用指定的內容,傳回從 GetStandardValues() 所傳回的標準值集合是否為可能值的獨占清單。

(繼承來源 TypeConverter)
GetStandardValuesSupported()

傳回這個物件是否支援可以從清單中選取的標準值集合。

(繼承來源 TypeConverter)
GetStandardValuesSupported(ITypeDescriptorContext)

會使用指定的內容傳回,這個物件是否支援可從清單中挑選的標準值集合。

(繼承來源 TypeConverter)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsValid(ITypeDescriptorContext, Object)

傳回指定值物件對這個類型和指定的內容是否有效。

(繼承來源 TypeConverter)
IsValid(Object)

會傳回指定值物件是否對這個型別有效。

(繼承來源 TypeConverter)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
SortProperties(PropertyDescriptorCollection, String[])

排序屬性的集合。

(繼承來源 TypeConverter)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱