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 类的新实例。 |