Browse Source

Fix bug in MetadataMethod.GetAttributes and add tests for PreserveSig in DllImportAttribute and PreserveSigAttribute.

pull/1317/head
Siegfried Pammer 7 years ago
parent
commit
12122841c6
  1. 22
      ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs
  2. 8
      ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemTestCase.cs
  3. 2
      ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataMethod.cs

22
ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs

@ -22,12 +22,10 @@ using System.Collections.Immutable;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using ICSharpCode.Decompiler.Metadata; using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem.Implementation; using ICSharpCode.Decompiler.TypeSystem.Implementation;
using NUnit.Framework; using NUnit.Framework;
@ -677,6 +675,26 @@ namespace ICSharpCode.Decompiler.Tests.TypeSystem
Assert.AreEqual((int)CharSet.Unicode, dllImport.NamedArguments.Single().Value); Assert.AreEqual((int)CharSet.Unicode, dllImport.NamedArguments.Single().Value);
} }
[Test]
public void DllImportAttributeWithPreserveSigFalse()
{
IMethod method = GetTypeDefinition(typeof(NonCustomAttributes)).Methods.Single(m => m.Name == "DoNotPreserveSig");
IAttribute dllImport = method.GetAttributes().Single();
Assert.AreEqual("System.Runtime.InteropServices.DllImportAttribute", dllImport.AttributeType.FullName);
Assert.AreEqual("unmanaged.dll", dllImport.FixedArguments[0].Value);
Assert.AreEqual(false, dllImport.NamedArguments.Single().Value);
}
[Test]
public void PreserveSigAttribute()
{
IMethod method = GetTypeDefinition(typeof(NonCustomAttributes)).Methods.Single(m => m.Name == "PreserveSigAsAttribute");
IAttribute preserveSig = method.GetAttributes().Single();
Assert.AreEqual("System.Runtime.InteropServices.PreserveSigAttribute", preserveSig.AttributeType.FullName);
Assert.IsTrue(preserveSig.FixedArguments.Length == 0);
Assert.IsTrue(preserveSig.NamedArguments.Length == 0);
}
[Test] [Test]
public void InOutParametersOnRefMethod() public void InOutParametersOnRefMethod()
{ {

8
ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemTestCase.cs

@ -153,6 +153,14 @@ namespace ICSharpCode.Decompiler.Tests.TypeSystem
[DllImport("unmanaged.dll", CharSet = CharSet.Unicode)] [DllImport("unmanaged.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DllMethod([In, Out] ref int p); public static extern bool DllMethod([In, Out] ref int p);
[DllImport("unmanaged.dll", PreserveSig = false)]
public static extern bool DoNotPreserveSig();
[PreserveSig]
public static void PreserveSigAsAttribute()
{
}
} }
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8)] [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8)]

2
ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataMethod.cs

@ -332,7 +332,7 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
if ((implAttributes & MethodImplAttributes.PreserveSig) == MethodImplAttributes.PreserveSig) { if ((implAttributes & MethodImplAttributes.PreserveSig) == MethodImplAttributes.PreserveSig) {
implAttributes &= ~MethodImplAttributes.PreserveSig; implAttributes &= ~MethodImplAttributes.PreserveSig;
} else { } else {
dllImport.AddNamedArg("PreserveSig", KnownTypeCode.Boolean, true); dllImport.AddNamedArg("PreserveSig", KnownTypeCode.Boolean, false);
} }
if ((info.Attributes & MethodImportAttributes.SetLastError) == MethodImportAttributes.SetLastError) if ((info.Attributes & MethodImportAttributes.SetLastError) == MethodImportAttributes.SetLastError)

Loading…
Cancel
Save