LocalizableAttribute 类
定义
指定是否应本地化某一属性或参数。Specifies whether a property or parameter 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
[<System.AttributeUsage(System.AttributeTargets.All)>]
type LocalizableAttribute = class
inherit Attribute
type LocalizableAttribute = class
inherit Attribute
Public NotInheritable Class LocalizableAttribute
Inherits Attribute
- 继承
- 属性
示例
下面的示例将 message 参数标记为需要本地化。The following example marks the message parameter as needing to be localized.
public static void ShowMessage([Localizable(true)] string message)
下面的示例将属性标记为需要本地化。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
下一个示例演示如何检查的值 LocalizableAttribute MyProperty 。The next example shows how to check the value of the LocalizableAttribute for MyProperty. 首先,该代码获取一个, PropertyDescriptorCollection 其中包含对象的所有属性。First, the code gets a PropertyDescriptorCollection with all the properties for the object. 然后,代码从获取 MyProperty PropertyDescriptorCollection 。Then, the code gets MyProperty from the PropertyDescriptorCollection. 接下来,它将返回此属性的特性并将其保存在特性变量中。Next, it returns the attributes for this property and saves them in the attributes variable.
最后,代码将设置 myAttribute 为中的值, LocalizableAttribute AttributeCollection 并检查属性是否需要本地化。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
注解
为组件生成代码时,使用设置的标记的成员将 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. 默认值为 false。The default is false.
备注
当您使用设置为的将属性标记为时 LocalizableAttribute true ,此属性的值将设置为常量成员 Yes 。When you mark a property with the LocalizableAttribute set to true, the value of this attribute is set to the constant member Yes. 对于标记有 LocalizableAttribute 设置为的的属性 false ,值为 No 。For a property marked with the LocalizableAttribute set to false, the value is No. 因此,当你想要在代码中检查此属性的值时,必须将属性指定为 LocalizableAttribute.Yes 或 LocalizableAttribute.No 。Therefore, 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.
构造函数
| LocalizableAttribute(Boolean) |
初始化 LocalizableAttribute 类的新实例。Initializes a new instance of the LocalizableAttribute class. |
字段
| Default |
指定默认值,即 No。Specifies the default value, which is No. 此 |
| No |
指定属性不应本地化。Specifies that a property should not be localized. 此 |
| Yes |
指定属性应本地化。Specifies that a property should be localized. 此 |
属性
| 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. (继承自 Attribute) |
方法
| Equals(Object) |
返回给定对象的值是否等于当前的 LocalizableAttribute。Returns whether the value of the given object is equal to the current LocalizableAttribute. |
| GetHashCode() |
返回此实例的哈希代码。Returns the hash code for this instance. |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 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. (继承自 Attribute) |
| Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (继承自 Attribute) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers. (继承自 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. (继承自 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1). (继承自 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object. (继承自 Attribute) |