private ImplementationMap RebuildAndLookup(ImplementationMap implementationMap)
        {
            using var stream = new MemoryStream();
            implementationMap.MemberForwarded.Module.Write(stream);

            var newModule = ModuleDefinition.FromBytes(stream.ToArray());
            var t         = newModule.TopLevelTypes.First(t => t.Name == nameof(PlatformInvoke));

            return(t.Methods.First(m => m.Name == implementationMap.MemberForwarded.Name).ImplementationMap);
        }
Beispiel #2
0
        private void AddImplementationMap(ImplementationMap implementationMap)
        {
            var table = (ImplementationMapTable)_tableStream.GetTable(MetadataTokenType.ImplMap);

            // Create and add row.
            var mapRow = new MetadataRow <ImplementationMapAttributes, uint, uint, uint>
            {
                Column1 = implementationMap.Attributes,
                Column2 = _tableStream.GetIndexEncoder(CodedIndex.MemberForwarded)
                          .EncodeToken(GetNewToken(implementationMap.MemberForwarded)),
                Column3 = _parentBuffer.StringStreamBuffer.GetStringOffset(implementationMap.ImportName),
                Column4 = GetModuleReferenceToken(implementationMap.ImportScope).Rid
            };

            table.Add(mapRow);
            _members.Add(implementationMap, mapRow.MetadataToken);
        }
Beispiel #3
0
        private void AddImplementationMap(MetadataToken ownerToken, ImplementationMap implementationMap)
        {
            if (implementationMap is null)
            {
                return;
            }

            var table   = Metadata.TablesStream.GetSortedTable <ImplementationMap, ImplementationMapRow>(TableIndex.ImplMap);
            var encoder = Metadata.TablesStream.GetIndexEncoder(CodedIndex.MemberForwarded);

            var row = new ImplementationMapRow(
                implementationMap.Attributes,
                encoder.EncodeToken(ownerToken),
                Metadata.StringsStream.GetStringIndex(implementationMap.Name),
                GetModuleReferenceToken(implementationMap.Scope).Rid);

            table.Add(implementationMap, row);
        }
        public void PersistentPInvokeMap()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;

            var image    = header.LockMetadata();
            var importer = new ReferenceImporter(image);
            var method   = CreateAndAddDummyMethod(image);
            var newMap   = new ImplementationMap(importer.ImportModule(new ModuleReference("SomeModule")),
                                                 "SomeImport",
                                                 ImplementationMapAttributes.CharSetUnicode);

            method.PInvokeMap  = newMap;
            method.Attributes |= MethodAttributes.PInvokeImpl;

            var mapping = header.UnlockMetadata();

            image  = header.LockMetadata();
            method = (MethodDefinition)image.ResolveMember(mapping[method]);
            Assert.NotNull(method.PInvokeMap);
            Assert.Equal(newMap.ImportScope, method.PInvokeMap.ImportScope, _comparer);
            Assert.Equal(newMap.ImportName, method.PInvokeMap.ImportName);
            Assert.Equal(newMap.Attributes, method.PInvokeMap.Attributes);
        }
Beispiel #5
0
 internal static void SetPInvokeMap(this IMemberForwarded owner, LazyValue <ImplementationMap> container, ImplementationMap newValue)
 {
     if (newValue != null && newValue.MemberForwarded != null)
     {
         throw new InvalidOperationException("Implementation map is already added to another member.");
     }
     if (container.Value != null)
     {
         container.Value.MemberForwarded = null;
     }
     container.Value = newValue;
     if (newValue != null)
     {
         newValue.MemberForwarded = owner;
     }
 }