Exemplo n.º 1
0
        public void ImportFieldFromExternalModuleShouldResultInMemberRef()
        {
            var type  = new TypeReference(_dummyAssembly, null, "Type");
            var field = new MemberReference(type, "Field",
                                            FieldSignature.CreateStatic(_module.CorLibTypeFactory.String));

            var result = _importer.ImportField(field);

            Assert.Equal(field, result, _comparer);
            Assert.Same(_module, result.Module);
        }
Exemplo n.º 2
0
        public void OperandTypeField()
        {
            var methodBody = CreateDummyMethodBody();
            var image      = methodBody.Method.Image;
            var importer   = new ReferenceImporter(image);

            var instructions = methodBody.Instructions;

            var simpleField = importer.ImportField(typeof(Type).GetField("EmptyTypes", BindingFlags.Public | BindingFlags.Static));

            instructions.AddRange(new[]
            {
                CilInstruction.Create(CilOpCodes.Ldsfld, simpleField),
                CilInstruction.Create(CilOpCodes.Pop),
                CilInstruction.Create(CilOpCodes.Ret),
            });

            var mapping = image.Header.UnlockMetadata();

            image = image.Header.LockMetadata();

            var newMethod = (MethodDefinition)image.ResolveMember(mapping[methodBody.Method]);

            instructions = newMethod.CilMethodBody.Instructions;

            Assert.Equal(simpleField, instructions[0].Operand as IMemberReference, _comparer);
        }
Exemplo n.º 3
0
        public void ImportExternalFieldDefinition()
        {
            var externalAssembly = Utilities.CreateTempNetAssembly();
            var metadataHeader   = externalAssembly.NetDirectory.MetadataHeader;
            var tableStream      = metadataHeader.GetStream <TableStream>();

            var type = new TypeDefinition("SomeNamespace", "SomeType");

            tableStream.GetTable <TypeDefinition>().Add(type);

            var field = new FieldDefinition("SomeField", FieldAttributes.Public | FieldAttributes.Static,
                                            new FieldSignature(metadataHeader.TypeSystem.String));

            type.Fields.Add(field);
            tableStream.GetTable <FieldDefinition>().Add(field);

            var assembly = Utilities.CreateTempNetAssembly();

            tableStream = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>();
            var importer = new ReferenceImporter(tableStream);

            var newReference = importer.ImportField(field) as MemberReference;

            Assert.AreNotSame(field, newReference,
                              "Imported field definition is the same object as the original.");

            Assert.IsTrue(_comparer.MatchMembers(field, newReference),
                          "Imported field definition does not match the original.");
        }
Exemplo n.º 4
0
        public void ImportReflectionField()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var image    = assembly.NetDirectory.MetadataHeader.LockMetadata();
            var importer = new ReferenceImporter(image);

            var reference = new MemberReference(
                CreateTypeReference(typeof(string)),
                "Empty",
                new FieldSignature(image.TypeSystem.String));

            var newReference = importer.ImportField(typeof(string).GetField("Empty", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public));

            VerifyImportedReference(image, reference, newReference);
        }
Exemplo n.º 5
0
        public void ImportReflectionField()
        {
            var assembly       = Utilities.CreateTempNetAssembly();
            var metadataHeader = assembly.NetDirectory.MetadataHeader;
            var tableStream    = metadataHeader.GetStream <TableStream>();
            var refTable       = tableStream.GetTable <MemberReference>();

            var importer = new ReferenceImporter(tableStream);

            var reference = new MemberReference(
                CreateTypeReference(typeof(string)),
                "Empty",
                new FieldSignature(metadataHeader.TypeSystem.String));

            var newReference = importer.ImportField(typeof(string).GetField("Empty", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public));

            VerifyImportedReference(tableStream, reference, newReference);
        }
Exemplo n.º 6
0
        public void ImportInternalFieldDefinition()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var image    = assembly.NetDirectory.MetadataHeader.LockMetadata();
            var importer = new ReferenceImporter(image);

            var type = new TypeDefinition("SomeNamespace", "SomeType");

            image.Assembly.Modules[0].TopLevelTypes.Add(type);

            var field = new FieldDefinition("SomeField", FieldAttributes.Public | FieldAttributes.Static,
                                            new FieldSignature(image.TypeSystem.String));

            type.Fields.Add(field);

            var newReference = importer.ImportField(field);

            Assert.Same(field, newReference);
        }
        /// <inheritdoc />
        public override object ResolveMember(MetadataToken token)
        {
            switch (token.Table)
            {
            case TableIndex.TypeDef:
                var type = _tokens[(int)token.Rid];
                if (type is RuntimeTypeHandle runtimeTypeHandle)
                {
                    return(_importer.ImportType(Type.GetTypeFromHandle(runtimeTypeHandle)));
                }
                break;

            case TableIndex.Field:
                var field = _tokens[(int)token.Rid];

                if (field is null)
                {
                    return(null);
                }

                if (field is RuntimeFieldHandle runtimeFieldHandle)
                {
                    return(_importer.ImportField(FieldInfo.GetFieldFromHandle(runtimeFieldHandle)));
                }

                if (field.GetType().FullName == "System.Reflection.Emit.GenericFieldInfo")
                {
                    bool result = FieldReader.TryReadField <RuntimeFieldHandle>(field, "m_field", out var mField);
                    var  ctx    = FieldReader.ReadField <RuntimeTypeHandle>(field, "m_context");
                    return(_importer.ImportField(FieldInfo.GetFieldFromHandle(result
                            ? mField
                            : FieldReader.ReadField <RuntimeFieldHandle>(field, "m_fieldHandle"), ctx)));
                }

                break;

            case TableIndex.Method:
            case TableIndex.MemberRef:
                var obj = _tokens[(int)token.Rid];

                if (obj is RuntimeMethodHandle methodHandle)
                {
                    return(_importer.ImportMethod(MethodBase.GetMethodFromHandle(methodHandle)));
                }

                if (obj.GetType().FullName == "System.Reflection.Emit.GenericMethodInfo")
                {
                    var  context   = FieldReader.ReadField <RuntimeTypeHandle>(obj, "m_context");
                    bool hasHandle = FieldReader.TryReadField <RuntimeMethodHandle>(obj, "m_method", out var mMethod);
                    var  mHandle   = FieldReader.ReadField <RuntimeMethodHandle>(obj, "m_methodHandle");
                    var  method    = MethodBase.GetMethodFromHandle(
                        hasHandle ? mMethod : mHandle,
                        context);

                    return(_importer.ImportMethod(method));
                }

                if (obj.GetType().FullName == "System.Reflection.Emit.VarArgMethod")
                {
                    return(_importer.ImportMethod(FieldReader.ReadField <MethodInfo>(obj, "m_method")));
                }

                break;

            case TableIndex.StandAloneSig:
                var reader = ByteArrayDataSource.CreateReader((byte[])_tokens[(int)token.Rid]);
                return(CallingConventionSignature.FromReader(new BlobReadContext(_readerContext), ref reader));
            }

            return(token);
        }