diff --git a/Mono.Cecil/IHaveAFileName.cs b/Mono.Cecil/IHaveAFileName.cs new file mode 100644 index 000000000..33b217e48 --- /dev/null +++ b/Mono.Cecil/IHaveAFileName.cs @@ -0,0 +1,9 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Mono.Cecil { + public interface IHaveAFileName { + string GetFileName (); + } +} diff --git a/Mono.Cecil/ModuleDefinition.cs b/Mono.Cecil/ModuleDefinition.cs index 8fb35a540..4e77eee02 100644 --- a/Mono.Cecil/ModuleDefinition.cs +++ b/Mono.Cecil/ModuleDefinition.cs @@ -1289,11 +1289,14 @@ public static bool HasImage (this ModuleDefinition self) public static string GetFileName (this Stream self) { - var file_stream = self as FileStream; - if (file_stream == null) + switch (self) { + case IHaveAFileName withFileName: + return withFileName.GetFileName (); + case FileStream fs: + return Path.GetFullPath (fs.Name); + default: return string.Empty; - - return Path.GetFullPath (file_stream.Name); + } } public static TargetRuntime ParseRuntime (this string self) diff --git a/Test/Mono.Cecil.Tests/ModuleTests.cs b/Test/Mono.Cecil.Tests/ModuleTests.cs index 5af2a65d7..93b165795 100644 --- a/Test/Mono.Cecil.Tests/ModuleTests.cs +++ b/Test/Mono.Cecil.Tests/ModuleTests.cs @@ -351,5 +351,20 @@ public void ExceptionInWriteDoesNotKeepLockOnFile () // Ensure you can still delete the file File.Delete (path); } + + class StreamWithAName : MemoryStream, IHaveAFileName { + public string GetFileName () + { + return "Yes I have!"; + } + } + + [Test] + public void StreamImplementingIHaveAFileNameShouldReturnItAsIs () + { + using (Stream stream = new StreamWithAName ()) { + Assert.AreEqual ("Yes I have!", stream.GetFileName ()); + } + } } }