LocalizableAttribute Class

Definition

指定是否应本地化某一属性。Specifies whether a property should be localized. 无法继承此类。This class cannot be inherited.

public ref class LocalizableAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class LocalizableAttribute : Attribute
public sealed class LocalizableAttribute : Attribute
type LocalizableAttribute = class
    inherit Attribute
Public NotInheritable Class LocalizableAttribute
Inherits Attribute
Inheritance
LocalizableAttribute
Attributes

Examples

下面的示例将属性标记为需要本地化。The following example marks a property as needing to be localized.

public:
   property int MyProperty 
   {
      [Localizable(true)]
      int get()
      {
         // Insert code here.
         return 0;
      }

      void set( int value )
      {
         // Insert code here.
      }
   }
[Localizable(true)]
 public int MyProperty {
    get {
       // Insert code here.
       return 0;
    }
    set {
       // Insert code here.
    }
 }
<Localizable(True)> _
Public Property MyProperty() As Integer
    Get
        ' Insert code here.
        Return 0
    End Get
    Set
        ' Insert code here.
    End Set 
End Property

下一个示例演示如何检查 MyPropertyLocalizableAttribute 的值。The next example shows how to check the value of the LocalizableAttribute for MyProperty. 首先,该代码使用对象的所有属性获取 PropertyDescriptorCollectionFirst, the code gets a PropertyDescriptorCollection with all the properties for the object. 然后,该代码将从 PropertyDescriptorCollection中获取 MyPropertyThen, the code gets MyProperty from the PropertyDescriptorCollection. 接下来,它将返回此属性的特性并将其保存在特性变量中。Next, it returns the attributes for this property and saves them in the attributes variable.

最后,该代码将 myAttribute 设置为 AttributeCollectionLocalizableAttribute 的值,并检查是否需要本地化该属性。Finally, the code sets myAttribute to the value of the LocalizableAttribute in the AttributeCollection and checks whether the property needs to be localized.

// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "MyProperty" ]->Attributes;

// Checks to see if the property needs to be localized.
LocalizableAttribute^ myAttribute = dynamic_cast<LocalizableAttribute^>(attributes[ LocalizableAttribute::typeid ]);
if ( myAttribute->IsLocalizable )
{
   // Insert code here.
}
// Gets the attributes for the property.
AttributeCollection attributes = 
TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;

// Checks to see if the property needs to be localized.
LocalizableAttribute myAttribute = 
(LocalizableAttribute)attributes[typeof(LocalizableAttribute)];
if(myAttribute.IsLocalizable) {
// Insert code here.
}
' Gets the attributes for the property.
Dim attributes As AttributeCollection = TypeDescriptor.GetProperties(Me)("MyProperty").Attributes

' Checks to see if the property needs to be localized.
Dim myAttribute As LocalizableAttribute = CType(attributes(GetType(LocalizableAttribute)), LocalizableAttribute)
If myAttribute.IsLocalizable Then
     ' Insert code here.
End If

Remarks

为组件生成代码时,标记有 LocalizableAttribute 设置为 true 的成员的属性值保存在资源文件中。When code is generated for a component, members that are marked with the LocalizableAttribute set to true have their property values saved in resource files. 无需修改代码即可本地化这些资源文件。You can localize these resource files without modifying the code.

默认情况下,如果数据类型允许,没有可本地化特性或标记为 LocalizableAttribute 设置为 false 的成员的属性值将保留为代码。By default, members that have no localizable attribute or are marked with the LocalizableAttribute set to false will have their property values persisted to code, if the data type allows. 否则,如果主组件设置为 Localizable,则所有属性都将保存到资源文件中。Otherwise, if the main component is set to Localizable, all properties will be persisted to the resource file. 默认为 falseThe default is false.

Note

LocalizableAttribute 设置为 true的属性标记时,此属性的值将设置为常量成员 YesWhen you mark a property with the LocalizableAttribute set to true, the value of this attribute is set to the constant member Yes. 对于标记为 LocalizableAttribute 设置为 false的属性,值为 NoFor a property marked with the LocalizableAttribute set to false, the value is No. 因此,当你想要在代码中检查此属性的值时,必须将属性指定为 LocalizableAttribute.YesLocalizableAttribute.NoTherefore, when you want to check the value of this attribute in your code, you must specify the attribute as LocalizableAttribute.Yes or LocalizableAttribute.No.

有关更多信息,请参阅特性For more information, see Attributes.

Constructors

LocalizableAttribute(Boolean)

初始化 LocalizableAttribute 类的新实例。Initializes a new instance of the LocalizableAttribute class.

Fields

Default

指定默认值,即 NoSpecifies the default value, which is No. static 字段是只读的。This static field is read-only.

No

指定属性不应本地化。Specifies that a property should not be localized. static 字段是只读的。This static field is read-only.

Yes

指定属性应本地化。Specifies that a property should be localized. static 字段是只读的。This static field is read-only.

Properties

IsLocalizable

获取一个值,该值指示属性是否应本地化。Gets a value indicating whether a property should be localized.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

Methods

Equals(Object)

返回给定对象的值是否等于当前的 LocalizableAttributeReturns whether the value of the given object is equal to the current LocalizableAttribute.

GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute()

确定此特性是否为默认特性。Determines if this attribute is the default.

IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

Applies to

See also