<bdo id='wntbY'></bdo><ul id='wntbY'></ul>
<tfoot id='wntbY'></tfoot>
  • <i id='wntbY'><tr id='wntbY'><dt id='wntbY'><q id='wntbY'><span id='wntbY'><b id='wntbY'><form id='wntbY'><ins id='wntbY'></ins><ul id='wntbY'></ul><sub id='wntbY'></sub></form><legend id='wntbY'></legend><bdo id='wntbY'><pre id='wntbY'><center id='wntbY'></center></pre></bdo></b><th id='wntbY'></th></span></q></dt></tr></i><div id='wntbY'><tfoot id='wntbY'></tfoot><dl id='wntbY'><fieldset id='wntbY'></fieldset></dl></div>
    <legend id='wntbY'><style id='wntbY'><dir id='wntbY'><q id='wntbY'></q></dir></style></legend>

  • <small id='wntbY'></small><noframes id='wntbY'>

      1. C# 属性及其用途

        C# Attributes and their uses(C# 属性及其用途)
        <tfoot id='Eyk69'></tfoot>
            <tbody id='Eyk69'></tbody>
          <i id='Eyk69'><tr id='Eyk69'><dt id='Eyk69'><q id='Eyk69'><span id='Eyk69'><b id='Eyk69'><form id='Eyk69'><ins id='Eyk69'></ins><ul id='Eyk69'></ul><sub id='Eyk69'></sub></form><legend id='Eyk69'></legend><bdo id='Eyk69'><pre id='Eyk69'><center id='Eyk69'></center></pre></bdo></b><th id='Eyk69'></th></span></q></dt></tr></i><div id='Eyk69'><tfoot id='Eyk69'></tfoot><dl id='Eyk69'><fieldset id='Eyk69'></fieldset></dl></div>

          <legend id='Eyk69'><style id='Eyk69'><dir id='Eyk69'><q id='Eyk69'></q></dir></style></legend>
              <bdo id='Eyk69'></bdo><ul id='Eyk69'></ul>

                • <small id='Eyk69'></small><noframes id='Eyk69'>

                  本文介绍了C# 属性及其用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我真的不太了解 C# 中的一般属性,我已经看到它们以许多不同的方式/地方使用,但我认为我没有看到其中一些的重要性.有些肯定很重要,因为它们提供了一个引人注目的功能,例如 [Serializable].然而,其他的似乎并不那么重要,例如我的同事用 [DataMember] 标记属性的一个.

                  I really don't know much about attributes in general in C#, I've seen them in use in a lot of different ways/places but I don't think I see the importance of some of them. Some definitely have importance because they provide a noticeable function, such as [Serializable]. Yet, others don't seem so important, such as one my coworker uses to mark properties with [DataMember].

                  我想我的问题是,什么是属性,它们有什么用处?有没有办法创建我自己的属性,我如何判断字段/方法/类/是否具有特定属性或在这些属性中设置了哪些值?

                  I suppose my question is, what are attributes and how are they useful? Is there a way to create my own attributes and how can I tell if fields/methods/classes/whatever have particular attributes or what values are set in those attributes?

                  推荐答案

                  什么是属性?

                  属性使您能够在描述该类型或方法的元数据中嵌入有关类型或方法的信息.

                  Attributes enable you to embed information about a type or method in the metadata which describes that type or method.

                  您通常希望使用属性描述关于类型或方法的机制的事实,而不是类型或方法的含义.例如,假设您有一个类型Employee.关于Employee的含义的一个事实是它是一种Person,一个Employee有一个Manager,等等.关于Employee 的机制 的一个事实是,它可以是数据绑定的目标,也可以序列化到磁盘,或其他任何东西.一个员工不能被序列化到磁盘,但是class Employee 可以.属性可让您将有关技术细节的信息与语义模型分开.

                  You typically want to use attributes to describe facts about the mechanism of the type or method rather than the meaning of the type or method. For example, suppose you have a type Employee. A fact about the meaning of Employee is that it is a kind of Person, that an Employee has a Manager, and so on. A fact about the mechanism of Employee is that it can be the target of data binding, or it can be serialized to disk, or whatever. An employee cannot be serialized to disk, but the class Employee can be. Attributes let you separate information about the technical details from the semantic model.

                  有没有办法创建我自己的属性?

                  Is there a way to create my own attributes?

                  是的.创建一个扩展属性的类.按照惯例,您希望将其命名为FooAttribute".如果这样做,您可以自行决定使用 [Foo] 语法或 [FooAttribute] 语法.

                  Yes. Create a class which extends Attribute. By convention you want to name it "FooAttribute". If you do so you can use either the [Foo] syntax or the [FooAttribute] syntax at your discretion.

                  我如何判断字段/方法/类/任何东西是否具有特定属性或在这些属性中设置了哪些值?

                  How can I tell if fields/methods/classes/whatever have particular attributes or what values are set in those attributes?

                  对反射对象使用 GetCustomAttributes 方法.

                  Use the GetCustomAttributes method on the reflection objects.

                  我应该在哪里阅读以获取更多信息?

                  Where should I read for more information?

                  从属性教程开始:

                  http://msdn.microsoft.com/en-us/library/aa288454(VS.71).aspx

                  然后阅读 C# 规范的第 17 章.

                  And then read all of chapter 17 of the C# specification.

                  这篇关于C# 属性及其用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                  Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)
                    <tbody id='sqrs1'></tbody>

                    1. <i id='sqrs1'><tr id='sqrs1'><dt id='sqrs1'><q id='sqrs1'><span id='sqrs1'><b id='sqrs1'><form id='sqrs1'><ins id='sqrs1'></ins><ul id='sqrs1'></ul><sub id='sqrs1'></sub></form><legend id='sqrs1'></legend><bdo id='sqrs1'><pre id='sqrs1'><center id='sqrs1'></center></pre></bdo></b><th id='sqrs1'></th></span></q></dt></tr></i><div id='sqrs1'><tfoot id='sqrs1'></tfoot><dl id='sqrs1'><fieldset id='sqrs1'></fieldset></dl></div>

                      <small id='sqrs1'></small><noframes id='sqrs1'>

                    2. <tfoot id='sqrs1'></tfoot>

                      • <bdo id='sqrs1'></bdo><ul id='sqrs1'></ul>
                            <legend id='sqrs1'><style id='sqrs1'><dir id='sqrs1'><q id='sqrs1'></q></dir></style></legend>