public void PersistentNullArgument()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;
            var image    = header.LockMetadata();
            var importer = new ReferenceImporter(image);

            var customAttribute = new CustomAttribute(
                (ICustomAttributeType)importer.ImportMethod(
                    typeof(DisplayNameAttribute).GetConstructor(new[] { typeof(string) })),
                new CustomAttributeSignature(new[]
                                             { new CustomAttributeArgument(image.TypeSystem.String, new ElementSignature(null)) }));

            image.Assembly.CustomAttributes.Add(customAttribute);

            header.UnlockMetadata();

            image = header.LockMetadata();
            Assert.Single(image.Assembly.CustomAttributes);
            Assert.Null(image.Assembly.CustomAttributes[0].Signature.FixedArguments[0].Elements[0].Value);
        }
        public void PersistentConstructor()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;
            var image    = header.LockMetadata();
            var importer = new ReferenceImporter(image);

            var customAttribute = new CustomAttribute(
                (ICustomAttributeType)importer.ImportMethod(
                    typeof(AssemblyTitleAttribute).GetConstructor(new[] { typeof(string) })),
                new CustomAttributeSignature(new[]
                                             { new CustomAttributeArgument(image.TypeSystem.String, new ElementSignature("SomeArgument")) }));

            image.Assembly.CustomAttributes.Add(customAttribute);

            header.UnlockMetadata();

            image = header.LockMetadata();
            Assert.Single(image.Assembly.CustomAttributes);
            Assert.Equal(customAttribute.Constructor, image.Assembly.CustomAttributes[0].Constructor, _comparer);
        }
Esempio n. 3
0
        public void PersistentAttributes()
        {
            const PropertyAttributes newAttributes = PropertyAttributes.SpecialName | PropertyAttributes.RtSpecialName;

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image    = header.LockMetadata();
            var property = CreateAndAddDummyProperty(image);

            property.Attributes = newAttributes;

            var mapping     = header.UnlockMetadata();
            var propertyRow = header.GetStream <TableStream>().GetTable <PropertyDefinitionTable>()[(int)(mapping[property].Rid - 1)];

            Assert.Equal(newAttributes, propertyRow.Column1);

            image    = header.LockMetadata();
            property = (PropertyDefinition)image.ResolveMember(mapping[property]);
            Assert.Equal(newAttributes, property.Attributes);
        }
Esempio n. 4
0
        public void CircularReferenceTest()
        {
            var sourceAssembly = WindowsAssembly.FromFile(typeof(SimpleClass).Assembly.Location);
            var sourceImage    = sourceAssembly.NetDirectory.MetadataHeader.LockMetadata();

            var assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", false);
            var header   = assembly.NetDirectory.MetadataHeader;
            var image    = header.LockMetadata();
            var cloner   = new MemberCloner(image);

            var classA = sourceImage.Assembly.Modules[0].TopLevelTypes.First(x => x.Name == "ClassA");
            var classB = sourceImage.Assembly.Modules[0].TopLevelTypes.First(x => x.Name == "ClassB");

            var clonedTypes = cloner.CloneTypes(new[] { classA, classB });

            foreach (var type in clonedTypes)
            {
                image.Assembly.Modules[0].TopLevelTypes.Add(type);
            }

            var main = new MethodDefinition("Main", MethodAttributes.Public | MethodAttributes.Static,
                                            new MethodSignature(image.TypeSystem.Void));

            main.CilMethodBody = new CilMethodBody(main);

            main.CilMethodBody.Instructions.AddRange(new[]
            {
                CilInstruction.Create(CilOpCodes.Newobj, clonedTypes[0].Methods.First(x => x.Name == ".ctor")),
                CilInstruction.Create(CilOpCodes.Newobj, clonedTypes[1].Methods.First(x => x.Name == ".ctor")),
                CilInstruction.Create(CilOpCodes.Call, clonedTypes[0].Methods.First(x => x.Name == "Test")),
                CilInstruction.Create(CilOpCodes.Ret)
            });

            image.Assembly.Modules[0].TopLevelTypes[0].Methods.Add(main);
            image.ManagedEntrypoint = main;

            header.UnlockMetadata();

            _context.VerifyOutput(assembly, "MyPropertyA: MyPropertyB");
        }
Esempio n. 5
0
        public void PersistentSemantics()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image     = header.LockMetadata();
            var property  = CreateAndAddDummyProperty(image);
            var getMethod = new MethodDefinition("get_" + property.Name,
                                                 MethodAttributes.Public | MethodAttributes.SpecialName,
                                                 new MethodSignature(property.Signature.PropertyType));

            property.PropertyMap.Parent.Methods.Add(getMethod);
            property.Semantics.Add(new MethodSemantics(getMethod, MethodSemanticsAttributes.Getter));

            var mapping = header.UnlockMetadata();

            image    = header.LockMetadata();
            property = (PropertyDefinition)image.ResolveMember(mapping[property]);
            Assert.Single(property.Semantics);
            Assert.Equal(getMethod, property.Semantics[0].Method, _comparer);
            Assert.Equal(MethodSemanticsAttributes.Getter, property.Semantics[0].Attributes);
        }
Esempio n. 6
0
        public void CustomBlobs()
        {
            var assembly  = NetAssemblyFactory.CreateAssembly("SomeAssembly", false);
            var header    = assembly.NetDirectory.MetadataHeader;
            var image     = header.LockMetadata();
            var importer  = new ReferenceImporter(image);
            var writeLine = importer.ImportMethod(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }));

            var main = new MethodDefinition("Main", MethodAttributes.Public | MethodAttributes.Static,
                                            new MethodSignature(image.TypeSystem.Void));

            main.CilMethodBody = new CilMethodBody(main);
            main.CilMethodBody.Instructions.AddRange(new[]
            {
                CilInstruction.Create(CilOpCodes.Ldstr, "Hello, world!"),
                CilInstruction.Create(CilOpCodes.Call, writeLine),
                CilInstruction.Create(CilOpCodes.Ret)
            });

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

            image.ManagedEntrypoint = main;

            var extraBlobs = new BlobSignature[]
            {
                new DataBlobSignature(new byte[] { 1, 2, 3 }),
                new DataBlobSignature(new byte[] { 4, 5, 6 })
            };

            // Commit changes to assembly
            var builder = new CustomBuilder(extraBlobs);

            image.Header.UnlockMetadata(builder);

            var blobStream = header.GetStream <BlobStream>();

            Assert.Equal(new byte[] { 1, 2, 3 }, blobStream.GetBlobByOffset(builder.GetBlobOffset(extraBlobs[0])));
            Assert.Equal(new byte[] { 4, 5, 6 }, blobStream.GetBlobByOffset(builder.GetBlobOffset(extraBlobs[1])));
        }
        public void PersistentCulture()
        {
            const string newCulture = "en-GB";

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image     = header.LockMetadata();
            var reference = CreateAndAddDummyReference(image);

            reference.Culture = newCulture;

            var mapping     = header.UnlockMetadata();
            var assemblyRow = header.GetStream <TableStream>().GetTable <AssemblyReferenceTable>()[(int)(mapping[reference].Rid - 1)];

            Assert.Equal(newCulture, header.GetStream <StringStream>().GetStringByOffset(assemblyRow.Column8));

            image = header.LockMetadata();
            var newReference = (AssemblyReference)image.ResolveMember(mapping[reference]);

            Assert.Equal(newCulture, newReference.Culture);
        }
        public void PersistentHashValue()
        {
            var newHashValue = new DataBlobSignature(new byte[] { 0x1, 0x2, 0x3, 0x4 });

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image     = header.LockMetadata();
            var reference = CreateAndAddDummyReference(image);

            reference.HashValue = newHashValue;

            var mapping     = header.UnlockMetadata();
            var assemblyRow = header.GetStream <TableStream>().GetTable <AssemblyReferenceTable>()[(int)(mapping[reference].Rid - 1)];

            Assert.Equal(newHashValue.Data, header.GetStream <BlobStream>().GetBlobByOffset(assemblyRow.Column9));

            image = header.LockMetadata();
            var newReference = (AssemblyReference)image.ResolveMember(mapping[reference]);

            Assert.Equal(newHashValue.Data, newReference.HashValue.Data);
        }
Esempio n. 9
0
        public void PersistentGenericParameters()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image    = header.LockMetadata();
            var importer = new ReferenceImporter(image);

            var type = new TypeDefinition("SomeNamespace", "SomeType", TypeAttributes.Public,
                                          importer.ImportType(typeof(object)));

            type.GenericParameters.Add(new GenericParameter(0, "T1"));
            type.GenericParameters.Add(new GenericParameter(1, "T2"));
            image.Assembly.Modules[0].TopLevelTypes.Add(type);

            var mapping = header.UnlockMetadata();

            image = header.LockMetadata();
            var newType = (TypeDefinition)image.ResolveMember(mapping[type]);

            Assert.Equal(type.GenericParameters, newType.GenericParameters, _comparer);
        }
Esempio n. 10
0
        public void PersistentImplAttributes()
        {
            const MethodImplAttributes newAttributes = MethodImplAttributes.Native | MethodImplAttributes.Unmanaged;

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image  = header.LockMetadata();
            var method = CreateAndAddDummyMethod(image);

            method.ImplAttributes = newAttributes;

            var mapping   = header.UnlockMetadata();
            var methodRow = header.GetStream <TableStream>().GetTable <MethodDefinitionTable>()[(int)(mapping[method].Rid - 1)];

            Assert.Equal(newAttributes, methodRow.Column2);

            image = header.LockMetadata();
            var newMethod = (MethodDefinition)image.ResolveMember(mapping[method]);

            Assert.Equal(newAttributes, newMethod.ImplAttributes);
        }
Esempio n. 11
0
        public void PersistentDeclaringType()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image = header.LockMetadata();

            var type = new TypeDefinition(null, "MyType", TypeAttributes.Public, null);

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

            var @event = CreateAndAddDummyEvent(image);

            @event.EventMap.Parent.EventMap = null;
            type.EventMap = @event.EventMap;

            var mapping = header.UnlockMetadata();

            image  = header.LockMetadata();
            @event = (EventDefinition)image.ResolveMember(mapping[@event]);
            Assert.Equal(type, @event.DeclaringType, _comparer);
        }
Esempio n. 12
0
        public void PersistentAttributes()
        {
            const FieldAttributes newAttributes = FieldAttributes.Public | FieldAttributes.Static;

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image = header.LockMetadata();
            var field = CreateAndAddDummyField(image);

            field.Attributes = newAttributes;

            var mapping  = header.UnlockMetadata();
            var fieldRow = header.GetStream <TableStream>()
                           .GetTable <FieldDefinitionTable>()[(int)(mapping[field].Rid - 1)];

            Assert.Equal(newAttributes, fieldRow.Column1);

            image = header.LockMetadata();
            field = (FieldDefinition)image.ResolveMember(mapping[field]);
            Assert.Equal(newAttributes, field.Attributes);
        }
Esempio n. 13
0
        public void ImportGenericInstanceMethod()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var image    = assembly.NetDirectory.MetadataHeader.LockMetadata();
            var importer = new ReferenceImporter(image);

            var specification = new MethodSpecification(
                new MemberReference(
                    CreateTypeReference(typeof(Activator)),
                    "CreateInstance",
                    new MethodSignature(new GenericParameterSignature(GenericParameterType.Method, 0))
            {
                GenericParameterCount = 1
            }),
                new GenericInstanceMethodSignature(CreateTypeDefOrRef(typeof(Stream))));

            var newSpecification = importer.ImportMethod(specification);

            Assert.NotSame(specification, newSpecification);
            Assert.Equal(specification, newSpecification, _comparer);
            Assert.Equal(image, newSpecification.Image);
        }
        public void PersistentName()
        {
            const string value = "myArgument";

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image = header.LockMetadata();
            var param = CreateAndAddDummyParameter(image);

            param.Name = value;

            var mapping  = header.UnlockMetadata();
            var paramRow = header.GetStream <TableStream>().GetTable <ParameterDefinitionTable>()[(int)(mapping[param].Rid - 1)];

            Assert.Equal(value, header.GetStream <StringStream>().GetStringByOffset(paramRow.Column3));

            image = header.LockMetadata();
            var newParam = (ParameterDefinition)image.ResolveMember(mapping[param]);

            Assert.Equal(value, newParam.Name);
        }
        public void PersistentAttributes()
        {
            const ParameterAttributes value = ParameterAttributes.Optional;

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image = header.LockMetadata();
            var param = CreateAndAddDummyParameter(image);

            param.Attributes = value;

            var mapping  = header.UnlockMetadata();
            var paramRow = header.GetStream <TableStream>().GetTable <ParameterDefinitionTable>()[(int)(mapping[param].Rid - 1)];

            Assert.Equal(value, paramRow.Column1);

            image = header.LockMetadata();
            var newParam = (ParameterDefinition)image.ResolveMember(mapping[param]);

            Assert.Equal(value, newParam.Attributes);
        }
Esempio n. 16
0
        public void PersistentName()
        {
            const string newName = "NewName";

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image  = header.LockMetadata();
            var method = CreateAndAddDummyMethod(image);

            method.Name = newName;

            var mapping   = header.UnlockMetadata();
            var methodRow = header.GetStream <TableStream>().GetTable <MethodDefinitionTable>()[(int)(mapping[method].Rid - 1)];

            Assert.Equal(newName, header.GetStream <StringStream>().GetStringByOffset(methodRow.Column4));

            image = header.LockMetadata();
            var newMethod = (MethodDefinition)image.ResolveMember(mapping[method]);

            Assert.Equal(newName, newMethod.Name);
        }
        public void PersistentVersion()
        {
            var newVersion = new Version(1, 2, 3, 4);

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image = header.LockMetadata();

            image.Assembly.Version = newVersion;
            header.UnlockMetadata();

            var assemblyRow = header.GetStream <TableStream>().GetTable <AssemblyDefinitionTable>()[0];

            Assert.Equal(newVersion.Major, assemblyRow.Column2);
            Assert.Equal(newVersion.Minor, assemblyRow.Column3);
            Assert.Equal(newVersion.Build, assemblyRow.Column4);
            Assert.Equal(newVersion.Revision, assemblyRow.Column5);

            image = header.LockMetadata();
            Assert.Equal(newVersion, image.Assembly.Version);
        }
Esempio n. 18
0
        public void CloneVariableReferences()
        {
            var sourceAssembly = WindowsAssembly.FromFile(typeof(SimpleClass).Assembly.Location);
            var sourceImage    = sourceAssembly.NetDirectory.MetadataHeader.LockMetadata();

            var assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", false);
            var header   = assembly.NetDirectory.MetadataHeader;
            var image    = header.LockMetadata();
            var cloner   = new MemberCloner(image);

            var variablesClass = sourceImage.Assembly.Modules[0].TopLevelTypes.First(x => x.Name == "Variables");
            var clonedClass    = cloner.CloneType(variablesClass);

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

            foreach (var clonedMethod in clonedClass.Methods.Where(x => x.CilMethodBody != null))
            {
                var body = clonedMethod.CilMethodBody;

                if (body.Signature != null)
                {
                    var variables = ((LocalVariableSignature)body.Signature.Signature).Variables;

                    var originalBody      = variablesClass.Methods.First(x => x.Name == clonedMethod.Name).CilMethodBody;
                    var originalVariables = ((LocalVariableSignature)originalBody.Signature.Signature).Variables;

                    foreach (var instruction in body.Instructions.Where(x =>
                                                                        x.OpCode.OperandType == CilOperandType.InlineVar ||
                                                                        x.OpCode.OperandType == CilOperandType.ShortInlineVar))
                    {
                        var originalInstruction = originalBody.Instructions.GetByOffset(instruction.Offset);
                        Assert.NotNull(instruction.Operand);
                        Assert.Equal(originalVariables.IndexOf((VariableSignature)originalInstruction.Operand),
                                     variables.IndexOf((VariableSignature)instruction.Operand));
                    }
                }
            }
        }
Esempio n. 19
0
        public void PersistentFields()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image    = header.LockMetadata();
            var importer = new ReferenceImporter(image);

            var types = new List <TypeDefinition>();

            for (int i = 0; i < 5; i++)
            {
                var type = new TypeDefinition(null, "SomeType_" + i,
                                              TypeAttributes.Public | TypeAttributes.Abstract,
                                              importer.ImportType(typeof(object)));

                for (int j = 0; j < i; j++)
                {
                    var field = new FieldDefinition("SomeField_" + i + "_" + j,
                                                    FieldAttributes.Public,
                                                    new FieldSignature(image.TypeSystem.Int32));
                    type.Fields.Add(field);
                }

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

            var mapping = header.UnlockMetadata();

            image = header.LockMetadata();
            foreach (var type in types)
            {
                var newType = ((TypeDefinition)image.ResolveMember(mapping[type]));
                Assert.Equal(type.Fields.Count, type.Fields.Count);
                Assert.Equal(type.Fields, newType.Fields, _comparer);
            }
        }
Esempio n. 20
0
        public void ImportExternalMethodDefinition()
        {
            var externalAssembly = NetAssemblyFactory.CreateAssembly(ExternalAssemblyName, true);
            var externalImage    = externalAssembly.NetDirectory.MetadataHeader.LockMetadata();

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

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

            var method = new MethodDefinition("SomeMethod", MethodAttributes.Public | MethodAttributes.Static,
                                              new MethodSignature(new[] { externalImage.TypeSystem.String }, externalImage.TypeSystem.Void));

            type.Methods.Add(method);

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var image    = assembly.NetDirectory.MetadataHeader.LockMetadata();
            var importer = new ReferenceImporter(image);

            var newReference = importer.ImportMethod(method) as MemberReference;

            Assert.NotSame(method, newReference);
            Assert.Equal((IMemberReference)method, newReference, _comparer);
        }
Esempio n. 21
0
        public void PersistentClassLayout()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image    = header.LockMetadata();
            var importer = new ReferenceImporter(image);

            var type = new TypeDefinition("SomeNamespace", "SomeType", TypeAttributes.Public,
                                          importer.ImportType(typeof(object)));

            type.ClassLayout = new ClassLayout(20, 1);
            image.Assembly.Modules[0].TopLevelTypes.Add(type);

            var mapping = header.UnlockMetadata();

            image = header.LockMetadata();
            type  = (TypeDefinition)image.ResolveMember(mapping[type]);
            Assert.NotNull(type.ClassLayout);
            Assert.Same(type.ClassLayout.Parent, type);
            Assert.Equal(20u, type.ClassLayout.ClassSize);
            Assert.Equal(1u, type.ClassLayout.PackingSize);
        }
Esempio n. 22
0
        public void PersistentSemantics()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image     = header.LockMetadata();
            var @event    = CreateAndAddDummyEvent(image);
            var addMethod = new MethodDefinition("add_" + @event.Name,
                                                 MethodAttributes.Public | MethodAttributes.SpecialName,
                                                 new MethodSignature(new[] { new ReferenceImporter(image).ImportTypeSignature(@event.EventType) },
                                                                     image.TypeSystem.Void));

            @event.EventMap.Parent.Methods.Add(addMethod);
            @event.Semantics.Add(new MethodSemantics(addMethod, MethodSemanticsAttributes.Getter));

            var mapping = header.UnlockMetadata();

            image  = header.LockMetadata();
            @event = (EventDefinition)image.ResolveMember(mapping[@event]);
            Assert.Single(@event.Semantics);
            Assert.Equal(addMethod, @event.Semantics[0].Method, _comparer);
            Assert.Equal(MethodSemanticsAttributes.Getter, @event.Semantics[0].Attributes);
        }
Esempio n. 23
0
        public void MatchMethodDefWithRef()
        {
            const string methodName = "MyMethod";

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var image    = assembly.NetDirectory.MetadataHeader.LockMetadata();

            var typeRef = CreateTypeRef1();
            var typeDef = new TypeDefinition(typeRef.Namespace, typeRef.Name);

            image.Assembly.ImportAssemblyInfo(typeRef.ResolutionScope.GetAssembly());

            var methodDef = new MethodDefinition(methodName, MethodAttributes.Public, new MethodSignature(CreateTypeSig2()));

            typeDef.Methods.Add(methodDef);

            var match = new MemberReference(typeRef, methodName, new MethodSignature(CreateTypeSig2()));
            var fail1 = new MemberReference(CreateTypeRef3(), methodName, new MethodSignature(CreateTypeSig2()));
            var fail2 = new MemberReference(typeRef, methodName + "1", new MethodSignature(CreateTypeSig2()));
            var fail3 = new MemberReference(typeRef, methodName, new MethodSignature(CreateTypeSig3()));

            VerifyMatching(methodDef, match, fail1, fail2, fail3);
        }
Esempio n. 24
0
        public void PersistentAttributes()
        {
            const AssemblyAttributes newAttributes = AssemblyAttributes.Msil
                                                     | AssemblyAttributes.EnableJitCompileTracking;

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image     = header.LockMetadata();
            var reference = CreateAndAddDummyReference(image);

            reference.Attributes = newAttributes;

            var mapping     = header.UnlockMetadata();
            var assemblyRow = header.GetStream <TableStream>().GetTable <AssemblyReferenceTable>()[(int)(mapping[reference].Rid - 1)];

            Assert.Equal(newAttributes, assemblyRow.Column5);

            image = header.LockMetadata();
            var newReference = (AssemblyReference)image.ResolveMember(mapping[reference]);

            Assert.Equal(newAttributes, newReference.Attributes);
        }
Esempio n. 25
0
        public void PersistentDeclaringType()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image    = header.LockMetadata();
            var importer = new ReferenceImporter(image);

            var type = new TypeDefinition("MyNamespace", "MyType", TypeAttributes.Public, importer.ImportType(typeof(object)));

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

            var method = CreateAndAddDummyMethod(image);

            method.DeclaringType.Methods.Remove(method);
            type.Methods.Add(method);

            var mapping = header.UnlockMetadata();

            image  = header.LockMetadata();
            method = (MethodDefinition)image.ResolveMember(mapping[method]);
            Assert.Equal(type, method.DeclaringType, _comparer);
        }
Esempio n. 26
0
        public void ImportExternalFieldDefinition()
        {
            var externalAssembly = NetAssemblyFactory.CreateAssembly(ExternalAssemblyName, true);
            var externalImage    = externalAssembly.NetDirectory.MetadataHeader.LockMetadata();

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

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

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

            type.Fields.Add(field);

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var image    = assembly.NetDirectory.MetadataHeader.LockMetadata();
            var importer = new ReferenceImporter(image);

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

            Assert.NotSame(field, newReference);
            Assert.Equal((IMemberReference)field, newReference, _comparer);
        }
Esempio n. 27
0
        public void PersistentExtraData()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;
            var image    = header.LockMetadata();
            var importer = new ReferenceImporter(image);

            var extraData = new byte[] { 1, 2, 3, 4 };

            var customAttribute = new CustomAttribute(
                (ICustomAttributeType)importer.ImportMethod(
                    typeof(ObfuscationAttribute).GetConstructor(Type.EmptyTypes)),
                new CustomAttributeSignature {
                ExtraData = extraData
            });

            image.Assembly.CustomAttributes.Add(customAttribute);

            header.UnlockMetadata();

            image = header.LockMetadata();
            Assert.Equal(extraData, image.Assembly.CustomAttributes[0].Signature.ExtraData);
        }
Esempio n. 28
0
        public void PersistentDataMultipleResources()
        {
            var newData1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
            var newData2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image = header.LockMetadata();

            var resource1 = CreateDummyResource();

            resource1.Data = newData1;
            var resource2 = CreateDummyResource();

            resource2.Data = newData2;

            image.Assembly.Resources.Add(resource1);
            image.Assembly.Resources.Add(resource2);

            var mapping = header.UnlockMetadata();

            var manifestResourceTable = header.GetStream <TableStream>().GetTable <ManifestResourceTable>();

            var resourceRow1 = manifestResourceTable[(int)(mapping[resource1].Rid - 1)];

            Assert.Equal(newData1, header.NetDirectory.ResourcesManifest.GetResourceData(resourceRow1.Column1));
            var resourceRow2 = manifestResourceTable[(int)(mapping[resource2].Rid - 1)];

            Assert.Equal(newData2, header.NetDirectory.ResourcesManifest.GetResourceData(resourceRow2.Column1));

            image     = header.LockMetadata();
            resource1 = (ManifestResource)image.ResolveMember(mapping[resource1]);
            resource2 = (ManifestResource)image.ResolveMember(mapping[resource2]);
            Assert.Equal(newData1, resource1.Data);
            Assert.Equal(newData2, resource2.Data);
        }
Esempio n. 29
0
        public static void Main(string[] args)
        {
            // Create new assembly.
            var assembly = NetAssemblyFactory.CreateAssembly("SomeAssembly", false);
            var header   = assembly.NetDirectory.MetadataHeader;

            // Lock the metadata so that we can add and remove members safely.
            var image    = header.LockMetadata();
            var importer = new ReferenceImporter(image);

            // Create a native method.
            var nativeMethod = CreateNativeMethod(image);

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

            // Create a new type.
            var type = new TypeDefinition("SomeNamespace", "SomeType", importer.ImportType(typeof(object)));

            type.Attributes = TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.Sealed;
            image.Assembly.Modules[0].TopLevelTypes.Add(type);

            // Create a new main method.
            var mainMethod = CreateMainMethod(image, importer, nativeMethod);

            type.Methods.Add(mainMethod);
            image.ManagedEntrypoint = mainMethod;

            // Commit our changes.
            header.UnlockMetadata();

            // Save to disk!
            assembly.NetDirectory.Flags &= ~ImageNetDirectoryFlags.IlOnly; // Required for mixed mode apps.
            string outputPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "output.exe");

            assembly.Write(outputPath, new CompactNetAssemblyBuilder(assembly));
        }
        private static WindowsAssembly CreateTempAssembly()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, false);
            var image    = assembly.NetDirectory.MetadataHeader.LockMetadata();
            var importer = new ReferenceImporter(image);

            var type = new TypeDefinition(
                TypeNamespace,
                TypeName,
                TypeAttributes.Public,
                importer.ImportType(typeof(object)));

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

            var mainMethod = new MethodDefinition(MainMethodName, MethodAttributes.Public | MethodAttributes.Static,
                                                  new MethodSignature(new[] { importer.ImportTypeSignature(typeof(string[])) },
                                                                      image.TypeSystem.Void));

            type.Methods.Add(mainMethod);

            mainMethod.MethodBody   = new CilMethodBody(mainMethod);
            image.ManagedEntrypoint = mainMethod;
            return(assembly);
        }