Esempio n. 1
0
        private void InitFields()
        {
            if (_fields != null)
            {
                return;
            }

            if (IsFree)
            {
                _fields = new List <ClrInstanceField>();
                return;
            }

            DesktopRuntimeBase runtime   = DesktopHeap.DesktopRuntime;
            IFieldInfo         fieldInfo = runtime.GetFieldInfo(_constructedMT);

            if (fieldInfo == null)
            {
                // Fill fields so we don't repeatedly try to init these fields on error.
                _fields = new List <ClrInstanceField>();
                return;
            }

            _fields = new List <ClrInstanceField>((int)fieldInfo.InstanceFields);

            // Add base type's fields.
            if (BaseType != null)
            {
                foreach (var field in BaseType.Fields)
                {
                    _fields.Add(field);
                }
            }

            int   count     = (int)(fieldInfo.InstanceFields + fieldInfo.StaticFields) - _fields.Count;
            ulong nextField = fieldInfo.FirstField;
            int   i         = 0;

            MetaDataImport import = null;

            if (nextField != 0 && DesktopModule != null)
            {
                import = DesktopModule.GetMetadataImport();
            }

            while (i < count && nextField != 0)
            {
                IFieldData field = runtime.GetFieldData(nextField);
                if (field == null)
                {
                    break;
                }

                // We don't handle context statics.
                if (field.IsContextLocal)
                {
                    nextField = field.NextField;
                    continue;
                }

                // Get the name of the field.
                string          name     = null;
                FieldAttributes attr     = FieldAttributes.PrivateScope;
                int             sigLen   = 0;
                IntPtr          ppValue  = IntPtr.Zero;
                IntPtr          fieldSig = IntPtr.Zero;

                if (import != null)
                {
                    import.GetFieldProps((int)field.FieldToken, out name, out attr, out fieldSig, out sigLen, out int pdwCPlusTypeFlab, out ppValue);
                }

                // If we couldn't figure out the name, at least give the token.
                if (import == null || name == null)
                {
                    name = string.Format("<ERROR:{0:X}>", field.FieldToken);
                }

                // construct the appropriate type of field.
                if (field.IsThreadLocal)
                {
                    if (_threadStatics == null)
                    {
                        _threadStatics = new List <ClrThreadStaticField>((int)fieldInfo.ThreadStaticFields);
                    }

                    // TODO:  Renable when thread statics are fixed.
                    //m_threadStatics.Add(new RealTimeMemThreadStaticField(m_heap, field, name));
                }
                else if (field.IsStatic)
                {
                    if (_statics == null)
                    {
                        _statics = new List <ClrStaticField>();
                    }

                    // TODO:  Enable default values.

                    /*
                     * object defaultValue = null;
                     *
                     *
                     * FieldAttributes sdl = FieldAttributes.Static | FieldAttributes.HasDefault | FieldAttributes.Literal;
                     * if ((attr & sdl) == sdl)
                     *  Debugger.Break();
                     */
                    _statics.Add(new DesktopStaticField(DesktopHeap, field, this, name, attr, null, fieldSig, sigLen));
                }
                else // instance variable
                {
                    _fields.Add(new DesktopInstanceField(DesktopHeap, field, name, attr, fieldSig, sigLen));
                }

                i++;
                nextField = field.NextField;
            }

            _fields.Sort((a, b) => a.Offset.CompareTo(b.Offset));
        }