LocalizableAttribute 类

指定属性是否应本地化。无法继承此类。

**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.All)> _
Public NotInheritable Class LocalizableAttribute
    Inherits Attribute
用法
Dim instance As LocalizableAttribute
[AttributeUsageAttribute(AttributeTargets.All)] 
public sealed class LocalizableAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::All)] 
public ref class LocalizableAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public final class LocalizableAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.All) 
public final class LocalizableAttribute extends Attribute

备注

在为组件生成代码时,通过将 LocalizableAttribute 设置为 true 进行标记的成员将其属性值保存在资源文件中。可以在不修改代码的情况下本地化这些资源文件。

默认情况下,如果数据类型允许,没有可本地化属性的成员或通过将 LocalizableAttribute 设置为 false 进行标记的成员将其属性值保持为代码。否则,如果主组件设置为 Localizable,则所有属性都将存留到资源文件。默认为 false

提示

当使用设置为 trueLocalizableAttribute 标记某个属性 (Property) 时,此属性 (Attribute) 的值会被设置为常数成员 Yes。对于通过将 LocalizableAttribute 设置为 false 进行标记的属性,此值为 No。因此,若要检查代码中此属性的值,必须将该属性指定为 LocalizableAttribute.YesLocalizableAttribute.No

有关更多信息,请参见 属性 (Attribute) 概述利用属性扩展元数据

主题 位置
演练:开发和使用自定义服务器控件 创作 ASP.NET 控件
演练:开发和使用自定义服务器控件 使用 Visual Web Developer 生成应用程序

示例

下面的示例将属性标记为需要进行本地化。

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

      void set( int value )
      {
         // Insert code here.
      }
   }
/** @attribute Localizable(true)
 */
/** @property
 */
public int get_MyProperty()
{
    // Insert code here.
    return 0;
}//get_MyProperty

/** @property 
 */
public void set_MyProperty(int value)
{
    // Insert code here.
}//set_MyProperty
public Localizable(true)
function get MyProperty() : int{
  // Insert code here.  Property goes on getter only when a property has
  // both a getter and setter.
  return 0
}

function set MyProperty(value: int){
  // Insert code here.
}

下一个示例演示如何检查 MyPropertyLocalizableAttribute 的值。首先,代码获取具有该对象的所有属性的 PropertyDescriptorCollection。然后,代码从 PropertyDescriptorCollection 中获取 MyProperty。下一步,它返回该属性 (Property) 的属性 (Attribute),并将它们保存在属性 (Attribute) 变量中。

最后,代码将 myAttribute 设置为 AttributeCollectionLocalizableAttribute 的值,并检查该属性是否需要进行本地化。

' 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
// 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.
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).
    get_Item("MyProperty").get_Attributes();
// Checks to see if the property needs to be localized.
LocalizableAttribute myAttribute = (LocalizableAttribute)(attributes.
    get_Item(LocalizableAttribute.class.ToType()));
if (myAttribute.get_IsLocalizable()) {
    // Insert code here.
}
// Gets the attributes for the property.
var attributes : AttributeCollection = TypeDescriptor.GetProperties(this)["MyProperty"].Attributes

// Checks to see if the property needs to be localized.
var myAttribute : LocalizableAttribute = LocalizableAttribute(attributes(LocalizableAttribute))
if(myAttribute.IsLocalizable){
     // Insert code here.
}

继承层次结构

System.Object
   System.Attribute
    System.ComponentModel.LocalizableAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

LocalizableAttribute 成员
System.ComponentModel 命名空间
Attribute
PropertyDescriptor
AttributeCollection 类
PropertyDescriptorCollection