예제 #1
0
        public void NewFieldDefinition()
        {
            var module = ModuleDefinition.FromBytes(Properties.Resources.HelloWorld);

            // Create new field.
            var field = new FieldDefinition(
                "MyField",
                FieldAttributes.Public | FieldAttributes.Static,
                FieldSignature.CreateStatic(module.CorLibTypeFactory.Object));

            module.GetOrCreateModuleType().Fields.Add(field);

            // Rebuild.
            var builder = new ManagedPEImageBuilder();
            var result  = builder.CreateImage(module);

            // Assert valid token.
            var newToken = result.TokenMapping[field];

            Assert.NotEqual(0u, newToken.Rid);

            // Assert token resolves to the new field.
            var newModule = ModuleDefinition.FromImage(result.ConstructedImage);
            var newField  = (FieldDefinition)newModule.LookupMember(newToken);

            Assert.Equal(field.Name, newField.Name);
        }
예제 #2
0
        public void NewTypeReference()
        {
            var module = ModuleDefinition.FromBytes(Properties.Resources.HelloWorld);

            // Import arbitrary type as reference.
            var importer  = new ReferenceImporter(module);
            var reference = importer.ImportType(typeof(MemoryStream));

            // Ensure type ref is added to the module by adding a dummy field referencing it.
            module.GetOrCreateModuleType().Fields.Add(new FieldDefinition(
                                                          "MyField",
                                                          FieldAttributes.Public | FieldAttributes.Static,
                                                          FieldSignature.CreateStatic(reference.ToTypeSignature())));

            // Rebuild.
            var builder = new ManagedPEImageBuilder();
            var result  = builder.CreateImage(module);

            // Assert valid token.
            var newToken = result.TokenMapping[reference];

            Assert.NotEqual(0u, newToken.Rid);

            // Assert token resolves to the same type reference.
            var newModule    = ModuleDefinition.FromImage(result.ConstructedImage);
            var newReference = (TypeReference)newModule.LookupMember(newToken);

            Assert.Equal(reference.Namespace, newReference.Namespace);
            Assert.Equal(reference.Name, newReference.Name);
        }
예제 #3
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);
        }
        public void ResolveStringEmptyField()
        {
            var module = new ModuleDefinition("SomeModule.dll");

            var stringType = new TypeReference(module.CorLibTypeFactory.CorLibScope, "System", "String");
            var emptyField = new MemberReference(stringType, "Empty",
                                                 FieldSignature.CreateStatic(module.CorLibTypeFactory.String));

            var definition = _fwResolver.ResolveField(emptyField);

            Assert.NotNull(definition);
            Assert.Equal(emptyField.Name, definition.Name);
            Assert.Equal(emptyField.Signature, definition.Signature, _comparer);
        }
예제 #5
0
        public void ImportFieldFromSameModuleShouldResultInSameInstance()
        {
            var type = new TypeDefinition(null, "Type", TypeAttributes.Public);

            _module.TopLevelTypes.Add(type);

            var field = new FieldDefinition("Method", FieldAttributes.Public | FieldAttributes.Static,
                                            FieldSignature.CreateStatic(_module.CorLibTypeFactory.Void));

            type.Fields.Add(field);

            var result = _importer.ImportField(field);

            Assert.Same(field, result);
        }
 private static FieldDefinition CreateDummyField(ModuleDefinition module, string name)
 {
     return(new FieldDefinition(name,
                                FieldAttributes.Public | FieldAttributes.Static,
                                FieldSignature.CreateStatic(module.CorLibTypeFactory.Int32)));
 }