【问题标题】:Are there any other useful attributes for c# properties? [duplicate]c# 属性还有其他有用的属性吗? [复制]
【发布时间】:2010-09-17 15:09:07
【问题描述】:

可能重复:
Most Useful Attributes in C#

另外:

[DefaultValue(100)]
[Description("Some descriptive field here")]
public int MyProperty{get; set;}

还有哪些C# Attributes 对Properties 有用,学完这些我感觉自己错过了。

相关问题

Most Useful Attributes in C#

【问题讨论】:

  • 这与 Mitchel Sellers 刚刚在问题中提出的链接非常接近。接下来是有用的属性或类。枚举等:P

标签: c# properties attributes


【解决方案1】:
[Obsolete("This is an obsolete property")]

这是我的最爱之一。允许您将属性/方法标记为过时,这将在构建时导致编译器警告(可选地,编译器错误)。

【讨论】:

  • +1 你差不多是这么说的,但是这个属性在开发过程中作为程序员的注释也很有用。
【解决方案2】:

只有几个……

同步、内联等:

[MethodImpl]

组件模型:

[TypeDescriptor], [DisplayName], [Editor]

序列化:

[Serializable], [DataMember], [XmlElement], [XmlAttribute], [NonSerialized], etc

声明式安全性:

[PrincipalPermission]

所有的 COM 东西...

【讨论】:

    【解决方案3】:
    [Browsable]
    

    是我的最爱。 (MSDN)

    【讨论】:

    • [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 是我经常与 BrowsableAttribute 一起使用的东西。
    【解决方案4】:

    长期以来,我一直想要一份完整的 c# 属性列表,但从未在 MSDN 文档或任何地方找到一个列表。我认为这是他们文档中较弱的部分之一。

    如果我想从 xml 序列化中排除一个属性,我会使用 [XmlIgnore]。

    【讨论】:

    【解决方案5】:

    【讨论】:

      【解决方案6】:

      如果您在多语言 UI 中使用 DescriptionCategory,那么您可能会发现基于资源的版本很有用(反映自 System.Windows.Forms):

      [AttributeUsage(AttributeTargets.All)]
      internal sealed class SRDescriptionAttribute : DescriptionAttribute
      {
          private bool replaced;
      
          public SRDescriptionAttribute(string description) : base(description)
          {
          }
      
          public override string Description
          {
              get
              {
                  if (!this.replaced)
                  {
                      this.replaced = true;
                      base.DescriptionValue = SR.GetString(base.Description);
                  }
                  return base.Description;
              }
          }
      }
      
      [AttributeUsage(AttributeTargets.All)]
      internal sealed class SRCategoryAttribute : CategoryAttribute
      {
          public SRCategoryAttribute(string category) : base(category)
          {
          }
      
          protected override string GetLocalizedString(string value)
          {
              return SR.GetString(value);
          }
      }
      

      其中SR 是相应ResourceManager 的包装器。

      【讨论】:

        【解决方案7】:

        Localizable ListBindable 可能对自定义组件设计者很感兴趣。

        【讨论】:

          【解决方案8】:

          我经常在枚举中使用它。枚举中是否有“默认”或“未知”值,但您不一定希望绑定到控件,例如下拉列表?添加自定义属性或使用现有属性来表示应该/不应该可见的项目。

          我在具有事件代理和策略注入的框架上做了很多工作,而在使用额外的元数据或松散耦合的事件装饰事件时,属性是非常宝贵的。

          有一些相当新的工具,例如 PostSharp (http://www.postsharp.org/),可用于将行为封装在属性中。该网站上有几个很好的例子;令人惊讶的是,您可以通过这些模式编写代码变得多么简单。 . .

          【讨论】:

            猜你喜欢
            • 2014-10-24
            • 2011-06-13
            • 2019-07-09
            • 2018-10-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-20
            相关资源
            最近更新 更多