.NET Decompiler with support for PDB generation, ReadyToRun, Metadata (&more) - cross-platform!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

128 lines
4.3 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Shell;
using Mono.Cecil;
namespace ICSharpCode.ILSpy.AddIn.Commands
{
public class ILSpyParameters
{
public ILSpyParameters(IEnumerable<string> assemblyFileNames, params string[] arguments)
{
this.AssemblyFileNames = assemblyFileNames;
this.Arguments = arguments;
}
public IEnumerable<string> AssemblyFileNames { get; private set; }
public string[] Arguments { get; private set; }
}
abstract class ILSpyCommand
{
protected ILSpyAddInPackage owner;
protected ILSpyCommand(ILSpyAddInPackage owner, uint id)
{
this.owner = owner;
CommandID menuCommandID = new CommandID(GuidList.guidILSpyAddInCmdSet, (int)id);
OleMenuCommand menuItem = new OleMenuCommand(OnExecute, menuCommandID);
menuItem.BeforeQueryStatus += OnBeforeQueryStatus;
owner.MenuService.AddCommand(menuItem);
}
protected virtual void OnBeforeQueryStatus(object sender, EventArgs e)
{
}
protected abstract void OnExecute(object sender, EventArgs e);
protected string GetILSpyPath()
{
var basePath = Path.GetDirectoryName(typeof(ILSpyAddInPackage).Assembly.Location);
return Path.Combine(basePath, "ILSpy.exe");
}
protected void OpenAssembliesInILSpy(ILSpyParameters parameters)
{
if (parameters == null)
return;
foreach (string assemblyFileName in parameters.AssemblyFileNames) {
if (!File.Exists(assemblyFileName)) {
owner.ShowMessage("Could not find assembly '{0}', please ensure the project and all references were built correctly!", assemblyFileName);
return;
}
}
string commandLineArguments = Utils.ArgumentArrayToCommandLine(parameters.AssemblyFileNames.ToArray());
if (parameters.Arguments != null) {
commandLineArguments = string.Concat(commandLineArguments, " ", Utils.ArgumentArrayToCommandLine(parameters.Arguments));
}
System.Diagnostics.Process.Start(GetILSpyPath(), commandLineArguments);
}
protected string GetProjectOutputPath(EnvDTE.Project project, Microsoft.CodeAnalysis.Project roslynProject)
{
string outputFileName = Path.GetFileName(roslynProject.OutputFilePath);
//get the directory path based on the project file.
string projectPath = Path.GetDirectoryName(project.FullName);
//get the output path based on the active configuration
string projectOutputPath = project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString();
//combine the project path and output path to get the bin path
return Path.Combine(projectPath, projectOutputPath, outputFileName);
}
protected Dictionary<string, string> GetReferences(Microsoft.CodeAnalysis.Project parentProject)
{
var dict = new Dictionary<string, string>();
foreach (var reference in parentProject.MetadataReferences) {
using (var assemblyDef = AssemblyDefinition.ReadAssembly(reference.Display)) {
if (IsReferenceAssembly(assemblyDef)) {
dict.Add(assemblyDef.Name.Name, GacInterop.FindAssemblyInNetGac(assemblyDef.Name));
} else {
dict.Add(assemblyDef.Name.Name, reference.Display);
}
}
}
foreach (var projectReference in parentProject.ProjectReferences) {
var roslynProject = owner.Workspace.CurrentSolution.GetProject(projectReference.ProjectId);
var project = owner.DTE.Solution.Projects.OfType<EnvDTE.Project>().FirstOrDefault(p => p.FileName == roslynProject.FilePath);
if (roslynProject != null && project != null)
dict.Add(roslynProject.AssemblyName, GetProjectOutputPath(project, roslynProject));
}
return dict;
}
protected bool IsReferenceAssembly(AssemblyDefinition assemblyDef)
{
return assemblyDef.CustomAttributes.Any(ca => ca.AttributeType.FullName == "System.Runtime.CompilerServices.ReferenceAssemblyAttribute");
}
}
class OpenILSpyCommand : ILSpyCommand
{
static OpenILSpyCommand instance;
public OpenILSpyCommand(ILSpyAddInPackage owner)
: base(owner, PkgCmdIDList.cmdidOpenILSpy)
{
}
protected override void OnExecute(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(GetILSpyPath());
}
internal static void Register(ILSpyAddInPackage owner)
{
instance = new OpenILSpyCommand(owner);
}
}
}