1. <tfoot id='7XJrI'></tfoot><legend id='7XJrI'><style id='7XJrI'><dir id='7XJrI'><q id='7XJrI'></q></dir></style></legend>
      <i id='7XJrI'><tr id='7XJrI'><dt id='7XJrI'><q id='7XJrI'><span id='7XJrI'><b id='7XJrI'><form id='7XJrI'><ins id='7XJrI'></ins><ul id='7XJrI'></ul><sub id='7XJrI'></sub></form><legend id='7XJrI'></legend><bdo id='7XJrI'><pre id='7XJrI'><center id='7XJrI'></center></pre></bdo></b><th id='7XJrI'></th></span></q></dt></tr></i><div id='7XJrI'><tfoot id='7XJrI'></tfoot><dl id='7XJrI'><fieldset id='7XJrI'></fieldset></dl></div>
        <bdo id='7XJrI'></bdo><ul id='7XJrI'></ul>
    2. <small id='7XJrI'></small><noframes id='7XJrI'>

        如何获取我们的属性设置的属性名称?

        How to get name of property which our attribute is set?(如何获取我们的属性设置的属性名称?)
            <tbody id='lwfT9'></tbody>
              <bdo id='lwfT9'></bdo><ul id='lwfT9'></ul>
              1. <i id='lwfT9'><tr id='lwfT9'><dt id='lwfT9'><q id='lwfT9'><span id='lwfT9'><b id='lwfT9'><form id='lwfT9'><ins id='lwfT9'></ins><ul id='lwfT9'></ul><sub id='lwfT9'></sub></form><legend id='lwfT9'></legend><bdo id='lwfT9'><pre id='lwfT9'><center id='lwfT9'></center></pre></bdo></b><th id='lwfT9'></th></span></q></dt></tr></i><div id='lwfT9'><tfoot id='lwfT9'></tfoot><dl id='lwfT9'><fieldset id='lwfT9'></fieldset></dl></div>

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

              2. <tfoot id='lwfT9'></tfoot>
                • <legend id='lwfT9'><style id='lwfT9'><dir id='lwfT9'><q id='lwfT9'></q></dir></style></legend>
                  本文介绍了如何获取我们的属性设置的属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我将在不向属性传递任何参数的情况下执行此操作!有可能吗?

                  I'm going to do this without passing any parameter to attribute! Is it possible?

                  class MyAtt : Attribute {
                      string NameOfSettedProperty() {
                          //How do this? (Would be MyProp for example)
                      }
                  }
                  
                  class MyCls {
                      [MyAtt]
                      int MyProp { get { return 10; } }
                  }
                  

                  推荐答案

                  属性是应用于类型成员、类型本身、方法参数或程序集的元数据.为了让您能够访问元数据,您必须拥有原始成员本身才能使用 GetCustomAttributes 等,即您的 TypePropertyInfo 实例, FieldInfo

                  Attributes are metadata applied to members of a type, the type itself, method parameters, or the assembly. For you to have access to the metadata, you must have had the original member itself to user GetCustomAttributes etc, i.e. your instance of Type, PropertyInfo, FieldInfo etc.

                  在您的情况下,我实际上会将属性名称传递给属性本身:

                  In your case, I would actually pass the name of the property to the attribute itself:

                  public CustomAttribute : Attribute
                  {
                    public CustomAttribute(string propertyName)
                    {
                      this.PropertyName = propertyName;
                    }
                  
                    public string PropertyName { get; private set; }
                  }
                  
                  public class MyClass
                  {
                    [Custom("MyProperty")]
                    public int MyProperty { get; set; }
                  }
                  

                  这篇关于如何获取我们的属性设置的属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 中的代码生成属性相关联)

                    <tfoot id='s5qFZ'></tfoot>

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

                        <tbody id='s5qFZ'></tbody>
                    • <legend id='s5qFZ'><style id='s5qFZ'><dir id='s5qFZ'><q id='s5qFZ'></q></dir></style></legend>

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