Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom streams with file name info #734

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Mono.Cecil/IHaveAFileName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Mono.Cecil {
public interface IHaveAFileName {
string GetFileName ();
}
}
11 changes: 7 additions & 4 deletions Mono.Cecil/ModuleDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions Test/Mono.Cecil.Tests/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
}
}
}
}