使用反射来确定哪些字段是来头地产领域 [英] Using Reflection to determine which Fields are backing fields of a Property

查看:103
本文介绍了使用反射来确定哪些字段是来头地产领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用反射来绘制出对象。这些对象是在托管代码,但我无法查看其源代码,底层结构等方面比通过反射等。所有这一切的首要目标是对象的一个​​基本的存储器映射(在功能上SOS.dll DumpObject 相似!ObjSize 命令)。因此,我试图确定哪些成员正在重复计算既是一个字段和属性。

I'm using reflection to map out objects. These objects are in managed code but I have no visibility into their source code, underlying structure, etc. other than through reflection. The overarching goal of all this is a rudimentary memory map of an object (similar in functionality to SOS.dll DumpObject and !ObjSize commands). As such, I'm trying to determine which members are being "double counted" as both a field and a property.

例如:

public class CalendarEntry
{
    // private property 
    private DateTime date { get; set;}

    // public field 
    public string day = "DAY";
}

在映射显示:


  • 字段


    • k__BackingField


    • 日期

    如果为这样的类:

    public class CalendarEntry
    {
        // private field 
        private DateTime date;
    
        // public field 
        public string day = "DAY";
    
        // Public property exposes date field safely. 
        public DateTime Date
        {
            get
            {
                return date;
            }
            set
            {
                    date = value;
            }
        }
    }
    

    在映射显示:


    • 字段


      • 日期


      • 日期

      乍一看没有什么可以告诉你,日期属性的支持字段名为领域日期。我试图避免这种情况下的两倍计算时间会给我一个坏的内存大小近似。

      At first glance there's nothing to tell you that the Date property's "backing field" is the field named date. I'm trying to avoid counting date twice in this scenario since that will give me a bad memory size approximation.

      什么是更加令人困惑/复杂的是我遇到的情况,其属性并不总是有将通过上市相应的字段中键入.GetFields()的方法,所以我不能忽略所有属性完全。

      What's more confusing/complicated is I've come across scenarios where properties don't always have a corresponding field that will be listed through the Type.GetFields() method so I can't just ignore all properties completely.

      如何确定任何想法如果集合中的一个字段从 Type.GetFields返回()基本上是由 Type.GetProperties返回了一些相应属性的支持字段()

      Any ideas on how to determine if a field in the collection returned from Type.GetFields() is essentially the backing field of some corresponding property returned from Type.GetProperties()?

      编辑 - 我已经很难确定哪些条件的属性不会从 Type.GetFields返回集合中列出的相应字段( )。是任何人都熟悉这样的条件呢?

      Edit- I've had trouble determining what conditions a property will not have a corresponding field in listed in the collection returned from Type.GetFields(). Is anyone familiar with such conditions?

      编辑2 - 我发现时,将不包括财产的支持字段在收集从 Type.GetFields()。如果您有以下字符串的引擎盖下看:

      Edit 2- I found a good example of when a property's backing field would not be included in the collection returned from Type.GetFields(). When looking under the hood of a String you have the following:


      • 对象包含名为FirstChar

      • 房产
      • 对象包含一个名为字符数地产

      • 对象包含属性命名长度

      • 对象包含字段名为m_stringLength

      • 对象包含字段名为m_firstChar

      • 对象包含字段名为空

      • 对象包含字段名为TrimHead

      • 对象包含字段名为TrimTail

      • 对象包含字段名为TrimBoth

      • 对象包含字段名为charPtrAlignConst

      • 对象包含字段命名alignConst

      • Object contains Property named FirstChar
      • Object contains Property named Chars
      • Object contains Property named Length
      • Object contains Field named m_stringLength
      • Object contains Field named m_firstChar
      • Object contains Field named Empty
      • Object contains Field named TrimHead
      • Object contains Field named TrimTail
      • Object contains Field named TrimBoth
      • Object contains Field named charPtrAlignConst
      • Object contains Field named alignConst

      m_firstChar m_stringLength 是属性的支持领域 FirstChar 长度但是字符串的实际内容是在举行字符属性。这是一个可以被索引,以返回字符串中所有字符索引属性,但我找不到持有字符串的字符的对应字段。为什么这是有什么想法?或如何获得索引属性的支持字段?

      The m_firstChar and m_stringLength are the backing fields of the Properties FirstChar and Length but the actual contents of the string are held in the Chars property. This is an indexed property that can be indexed to return all the chars in the String but I can't find a corresponding field that holds the characters of a string. Any thoughts on why that is? Or how to get the backing field of the indexed property?

      推荐答案

      属性的后备字段的名称是一个编译器实现细节, ,总能在未来改变,即使你找出模式

      The name of a property's backing field is a compiler implementation detail and can always change in the future, even if you figure out the pattern.

      我想你已经打在回答你的问题:忽略所有属性

      I think you've already hit on the answer to your question: ignore all properties.

      请记住,一个属性是变相的只是一个或两个功能。一个属性只会有一个编译器生成支持字段时,由源代码具体要求。例如,在C#中:

      Remember that a property is just one or two functions in disguise. A property will only have a compiler generated backing field when specifically requested by the source code. For example, in C#:

      public string Foo { get; set; }
      



      但是,一个类的创建者不需要使用编译器生成的属性是这样的。例如,属性可能会得到一个恒定值,多属性可能获取/设置位域的不同部分,等等。在这种情况下,你不会希望看到一个单一的支持字段为每个属性。它也可以忽略这些属性。您的代码不会错过任何实际的数据。

      But the creator of a class need not use compiler generated properties like this. For example, a property might get a constant value, multiple properties might get/set different portions of a bit field, and so on. In these cases, you wouldn't expect to see a single backing field for each property. It's fine to ignore these properties. Your code won't miss any actual data.

      这篇关于使用反射来确定哪些字段是来头地产领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆