Esempio n. 1
0
        private void InitializeTypeFields()
        {
            this.TypeFields = new Dictionary <FieldDef, ITypeDefOrRef>();
            MethodDef cctor = this.Type.FindMethod(".cctor");

            if (cctor == null)
            {
                throw new Exception("Unable to find virtualization type .cctor");
            }

            if (!cctor.HasBody || !cctor.Body.HasInstructions)
            {
                throw new Exception("Virtualization type .cctor has no instructions");
            }

            var subs = cctor.FindAll(new Code[] { Code.Ldtoken, Code.Call, Code.Stsfld });

            foreach (var sub in subs)
            {
                FieldDef field = sub[2].Operand as FieldDef;
                if (field == null)
                {
                    continue;
                }

                ITypeDefOrRef type = sub[0].Operand as ITypeDefOrRef;
                if (type == null)
                {
                    continue;
                }

                if (this.TypeFields.ContainsKey(field))
                {
                    Console.WriteLine("[InitializeTypeFields] WARNING: Overwriting ITypeDefOrRef for FieldDef");
                    this.TypeFields[field] = type;
                }
                else
                {
                    this.TypeFields.Add(field, type);
                }
            }
        }