• <legend id='nVaKI'><style id='nVaKI'><dir id='nVaKI'><q id='nVaKI'></q></dir></style></legend>
  • <small id='nVaKI'></small><noframes id='nVaKI'>

      <bdo id='nVaKI'></bdo><ul id='nVaKI'></ul>

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

        如何获取自定义属性?

        How do I GetCustomAttributes?(如何获取自定义属性?)

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

          <legend id='b2KLI'><style id='b2KLI'><dir id='b2KLI'><q id='b2KLI'></q></dir></style></legend>

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

            • <bdo id='b2KLI'></bdo><ul id='b2KLI'></ul>

                  本文介绍了如何获取自定义属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经使用 2.0 框架尝试了以下代码,并且我得到了一个属性,但是当我在紧凑框架上尝试这个时,它总是返回一个空数组.MSDN 文档说它支持,我做错了吗?

                  I have tried the following code using the 2.0 framework and I get an attribute back, but when I try this on the compact framework, it always returns an empty array. The MSDN documenation says its supported, am I doing something wrong?

                    Test x = new Test();
                    FieldInfo field_info = x.GetType().GetField("ArrayShorts");
                    object[] custom_attributes = field_info.GetCustomAttributes(typeof(MarshalAsAttribute), false);
                  
                    [StructLayout(LayoutKind.Sequential)]
                    public struct Test
                    {
                       [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
                       public ushort[] ArrayShorts;
                    }
                  

                  推荐答案

                  EDIT 2

                  所以我现在正在与 CF 团队核实,但我相信你发现了一个错误.这显示得更好:

                  So I'm checking with the CF team now but I believe you've found a bug. This shows it even better:

                  public class MyAttribute : Attribute
                  {
                      public MyAttribute(UnmanagedType foo)
                      {
                      }
                  
                      public int Bar { get; set; }
                  }
                  
                  [StructLayout(LayoutKind.Sequential)]
                  public struct Test
                  {
                      [CLSCompliant(false)]
                      [MyAttribute(UnmanagedType.ByValArray, Bar = 4)]
                      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
                      public ushort[] ArrayShorts;
                  }
                  
                  class Program
                  {
                      static void Main(string[] args)
                      {
                  
                          FieldInfo field_info = typeof(Test).GetField("ArrayShorts");
                          object[] custom_attributes = field_info.GetCustomAttributes(typeof(MarshalAsAttribute), false);
                          Debug.WriteLine("Attributes: " + custom_attributes.Length.ToString());
                          custom_attributes = field_info.GetCustomAttributes(typeof(MyAttribute), false);
                          Debug.WriteLine("Attributes: " + custom_attributes.Length.ToString());
                          custom_attributes = field_info.GetCustomAttributes(typeof(CLSCompliantAttribute), false);
                          Debug.WriteLine("Attributes: " + custom_attributes.Length.ToString());
                      }
                  }
                  

                  在完整的框架下,我得到了这个:

                  Under the full framework I get back this:

                  Attributes: 1
                  Attributes: 1
                  Attributes: 1
                  

                  在 CF 3.5 下我得到这个:

                  Under CF 3.5 I get this:

                  Attributes: 0
                  Attributes: 1
                  Attributes: 1
                  

                  因此您可以看到它完全能够返回自定义或 BCL 中的属性,而不是 MarshalAsAttribute.

                  So you can see it's fully capable of returning an attribute, either custom or within the BCL, just not the MarshalAsAttribute.

                  编辑 3好吧,我做了更多的挖掘,结果发现 CF 行为实际上是 正确如果你按照规范进行.这违背了所有逻辑,但它是正确的.

                  EDIT 3 Alright, I did a little more digging, and it turns out that the CF behavior is actually correct if you go by the spec. It goes against all logic, but it's right.

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

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

                  相关文档推荐

                  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='HYF4J'></tbody>
                  <i id='HYF4J'><tr id='HYF4J'><dt id='HYF4J'><q id='HYF4J'><span id='HYF4J'><b id='HYF4J'><form id='HYF4J'><ins id='HYF4J'></ins><ul id='HYF4J'></ul><sub id='HYF4J'></sub></form><legend id='HYF4J'></legend><bdo id='HYF4J'><pre id='HYF4J'><center id='HYF4J'></center></pre></bdo></b><th id='HYF4J'></th></span></q></dt></tr></i><div id='HYF4J'><tfoot id='HYF4J'></tfoot><dl id='HYF4J'><fieldset id='HYF4J'></fieldset></dl></div>

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

                  1. <tfoot id='HYF4J'></tfoot>

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