Skip to content

Add basic code of IL with C# #545

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

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
54 changes: 53 additions & 1 deletion backend/ILSpyX.Backend/Decompiler/DecompilerBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ICSharpCode.Decompiler.Disassembler;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.Util;
using ICSharpCode.ILSpyX;
using ILSpyX.Backend.Application;
using ILSpyX.Backend.Model;
Expand All @@ -14,13 +15,15 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Threading;
using System.Threading.Tasks;

namespace ILSpyX.Backend.Decompiler;

using ICSharpCode.ILSpyX.Extensions;
using System.Reflection.Metadata;

public class DecompilerBackend(
ILoggerFactory loggerFactory,
ILSpyBackendSettings ilspyBackendSettings,
Expand Down Expand Up @@ -165,6 +168,7 @@ public DecompileResult GetCode(string? assemblyPath, EntityHandle handle, string
return outputLanguage switch
{
LanguageName.IL => DecompileResult.WithCode(GetILCode(assemblyPath, handle)),
LanguageName.ILCharp => DecompileResult.WithCode(GetIlWithCSharpCode(assemblyPath, handle, LanguageName.CSharpLatest)),
_ => DecompileResult.WithCode(GetCSharpCode(assemblyPath, handle, outputLanguage))
};
}
Expand Down Expand Up @@ -229,6 +233,54 @@ private string GetCSharpCode(string assemblyPath, EntityHandle handle, string ou
return string.Empty;
}

private string GetIlWithCSharpCode(string assemblyPath, EntityHandle handle, string outputLanguage)
{
if (handle.IsNil)
{
return string.Empty;
}

var decompiler = CreateDecompiler(assemblyPath, outputLanguage);
if (decompiler is null)
{
return string.Empty;
}

var module = decompiler.TypeSystem.MainModule;
var textOutput = new PlainTextOutput();
var disassembler = CreateDisassembler(assemblyPath, module, textOutput);
string code;

switch (handle.Kind)
{
case HandleKind.AssemblyDefinition:
code = GetAssemblyCode(assemblyPath, decompiler);
GetAssemblyILCode(disassembler, assemblyPath, module, textOutput);
return textOutput.ToString() + code;
case HandleKind.TypeDefinition:
var typeDefinition = module.GetDefinition((TypeDefinitionHandle) handle);
disassembler.DisassembleType(module.MetadataFile, (TypeDefinitionHandle) handle);
if (typeDefinition.DeclaringType == null)
code = decompiler.DecompileTypesAsString(new[] { (TypeDefinitionHandle) handle });
else
code = decompiler.DecompileAsString(handle);
return textOutput.ToString() + code;
case HandleKind.FieldDefinition:
disassembler.DisassembleField(module.MetadataFile, (FieldDefinitionHandle) handle);
return textOutput.ToString() + decompiler.DecompileAsString(handle);
case HandleKind.MethodDefinition:
disassembler.DisassembleMethod(module.MetadataFile, (MethodDefinitionHandle) handle);
return textOutput.ToString() + decompiler.DecompileAsString(handle);
case HandleKind.PropertyDefinition:
disassembler.DisassembleProperty(module.MetadataFile, (PropertyDefinitionHandle) handle);
return textOutput.ToString() + decompiler.DecompileAsString(handle);
case HandleKind.EventDefinition:
disassembler.DisassembleEvent(module.MetadataFile, (EventDefinitionHandle) handle);
return textOutput.ToString() + decompiler.DecompileAsString(handle);
}
return string.Empty;
}

private string GetILCode(string assemblyPath, EntityHandle handle)
{
if (handle.IsNil)
Expand Down
1 change: 1 addition & 0 deletions backend/ILSpyX.Backend/Model/LanguageNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class LanguageName
public const string CSharp_10 = "cs-10";
public const string CSharp_11 = "cs-11";
public const string CSharp_12 = "cs-12";
public const string ILCharp = "il-csharp";
public const string CSharpLatest = CSharp_12;

}
3 changes: 2 additions & 1 deletion vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"C# 3.0 / VS 2008",
"C# 2.0 / VS 2005",
"C# 1.0 / VS .NET",
"IL"
"IL",
"IL with C#"
],
"enumDescriptions": [
"Decompile to C# 12.0 by default",
Expand Down
1 change: 1 addition & 0 deletions vscode-extension/src/decompiler/languageInfos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface LanguageInfo {

export const languageInfos = createLanguageMap([
{ name: LanguageName.IL, displayName: "IL", vsLanguageMode: "il" },
{ name: LanguageName.IL_CSharp, displayName: "IL With C#", vsLanguageMode: "il" },
{
name: LanguageName.CSharp_1,
displayName: "C# 1.0 / VS .NET",
Expand Down
1 change: 1 addition & 0 deletions vscode-extension/src/protocol/LanguageName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export enum LanguageName {
IL = "il",
IL_CSharp = "il-csharp",
CSharp_1 = "cs-1",
CSharp_2 = "cs-2",
CSharp_3 = "cs-3",
Expand Down