DefaultSettingValueAttribute 类

定义

指定应用程序设置属性的默认值。

public ref class DefaultSettingValueAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Property)]
public sealed class DefaultSettingValueAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Property)>]
type DefaultSettingValueAttribute = class
    inherit Attribute
Public NotInheritable Class DefaultSettingValueAttribute
Inherits Attribute
继承
DefaultSettingValueAttribute
属性

示例

下面的代码示例演示如何使用 DefaultSettingValueAttribute 应用于包装类的四个属性中的 FormSettings 三个属性,该属性派生自 ApplicationSettingsBase 类。 此类用于保留窗体的位置、大小、背景色和文本。 这些窗体属性的前三个具有与之关联的默认值。

类概述中 ApplicationSettingsBase 列出了完整的代码示例。

//Application settings wrapper class
ref class FormSettings sealed: public ApplicationSettingsBase
{
public:
    [UserScopedSettingAttribute()]
    property String^ FormText
    {
        String^ get()
        {
            return (String^)this["FormText"];
        }
        void set( String^ value )
        {
            this["FormText"] = value;
        }
    }

public:
    [UserScopedSettingAttribute()]
    [DefaultSettingValueAttribute("0, 0")]
    property Point FormLocation
    {
        Point get()
        {
            return (Point)(this["FormLocation"]);
        }
        void set( Point value )
        {
            this["FormLocation"] = value;
        }
    }

public:
    [UserScopedSettingAttribute()]
    [DefaultSettingValueAttribute("225, 200")]
    property Size FormSize
    {
        Size get()
        {
            return (Size)this["FormSize"];
        }
        void set( Size value )
        {
            this["FormSize"] = value;
        }
    }

public:
    [UserScopedSettingAttribute()]
    [DefaultSettingValueAttribute("LightGray")]
    property Color FormBackColor
    {
        Color get()
        {
            return (Color)this["FormBackColor"];
        }
        void set(Color value)
        {
            this["FormBackColor"] = value;
        }
    }

};
//Application settings wrapper class
sealed class FormSettings : ApplicationSettingsBase
{
    [UserScopedSettingAttribute()]
    public String FormText
    {
        get { return (String)this["FormText"]; }
        set { this["FormText"] = value; }
    }

    [UserScopedSettingAttribute()]
    [DefaultSettingValueAttribute("0, 0")]
    public Point FormLocation
    {
        get { return (Point)(this["FormLocation"]); }
        set { this["FormLocation"] = value; }
    }

    [UserScopedSettingAttribute()]
    [DefaultSettingValueAttribute("225, 200")]
    public Size FormSize
    {
        get { return (Size)this["FormSize"]; }
        set { this["FormSize"] = value; }
    }

    [UserScopedSettingAttribute()]
    [DefaultSettingValueAttribute("LightGray")]
    public Color FormBackColor
    {
        get { return (Color)this["FormBackColor"]; }
        set { this["FormBackColor"] = value; }
    }
}
'Application settings wrapper class. This class defines the settings we intend to use in our application.
NotInheritable Class FormSettings
    Inherits ApplicationSettingsBase

    <UserScopedSettingAttribute()> _
    Public Property FormText() As String
        Get
            Return CStr(Me("FormText"))
        End Get
        Set(ByVal value As String)
            Me("FormText") = value
        End Set
    End Property

    <UserScopedSettingAttribute(), DefaultSettingValueAttribute("0, 0")> _
    Public Property FormLocation() As Point
        Get
            Return CType(Me("FormLocation"), Point)
        End Get
        Set(ByVal value As Point)
            Me("FormLocation") = value
        End Set
    End Property

    <UserScopedSettingAttribute(), DefaultSettingValueAttribute("225, 200")> _
    Public Property FormSize() As Size
        Get
            Return CType(Me("FormSize"), Size)
        End Get
        Set(ByVal value As Size)
            Me("FormSize") = value
        End Set
    End Property

    <UserScopedSettingAttribute(), DefaultSettingValueAttribute("LightGray")> _
    Public Property FormBackColor() As Color
        Get
            Return CType(Me("FormBackColor"), Color)
        End Get
        Set(ByVal value As Color)
            Me("FormBackColor") = value
        End Set
    End Property
End Class

注解

重要

DefaultSettingValueAttribute 只能应用于单个设置属性;将此属性应用于整个应用程序设置类无效。

不同的设置提供程序对 的使用 DefaultSettingValueAttribute可能有不同的要求或限制。 例如, LocalFileSettingsProvider 不需要此属性,如果数据存储中已存在任何值(默认值或用户修改值),则会替代此属性提供的任何值。

DefaultSettingValueAttribute 要求默认值可以表示为字符串。 因此,使用 XML 序列化的设置不能通过此属性指定默认值。 某些提供程序可能会选择支持多个序列化方案,这些方案可以在编译时使用 SettingsSerializeAsAttribute指定。

注意

此属性指定的默认值以纯文本形式存储在生成的编译.exe或.dll文件中。 因此,这些默认值本质上是不安全的。

构造函数

DefaultSettingValueAttribute(String)

初始化 DefaultSettingValueAttribute 类的实例。

属性

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。

(继承自 Attribute)
Value

获取应用程序设置属性的默认值。

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅