GenericEnumConverter 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在字串和列舉型別之間轉換。
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 類別的新執行個體。 |