|
|
|
|
@ -46,12 +46,12 @@ namespace ICSharpCode.ILSpy
@@ -46,12 +46,12 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
string name = "C#"; |
|
|
|
|
bool showAllMembers = false; |
|
|
|
|
Predicate<IAstTransform> transformAbortCondition = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CSharpLanguage() |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
internal static IEnumerable<CSharpLanguage> GetDebugLanguages() |
|
|
|
|
{ |
|
|
|
|
DecompilerContext context = new DecompilerContext(ModuleDefinition.CreateModule("dummy", ModuleKind.Dll)); |
|
|
|
|
@ -70,20 +70,23 @@ namespace ICSharpCode.ILSpy
@@ -70,20 +70,23 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
showAllMembers = true |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
public override string Name { |
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
public override string Name |
|
|
|
|
{ |
|
|
|
|
get { return name; } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override string FileExtension { |
|
|
|
|
|
|
|
|
|
public override string FileExtension |
|
|
|
|
{ |
|
|
|
|
get { return ".cs"; } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override string ProjectFileExtension { |
|
|
|
|
|
|
|
|
|
public override string ProjectFileExtension |
|
|
|
|
{ |
|
|
|
|
get { return ".csproj"; } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options) |
|
|
|
|
{ |
|
|
|
|
WriteCommentLine(output, TypeToString(method.DeclaringType, includeNamespace: true)); |
|
|
|
|
@ -92,7 +95,7 @@ namespace ICSharpCode.ILSpy
@@ -92,7 +95,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
codeDomBuilder.RunTransformations(transformAbortCondition); |
|
|
|
|
codeDomBuilder.GenerateCode(output); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void DecompileProperty(PropertyDefinition property, ITextOutput output, DecompilationOptions options) |
|
|
|
|
{ |
|
|
|
|
WriteCommentLine(output, TypeToString(property.DeclaringType, includeNamespace: true)); |
|
|
|
|
@ -101,7 +104,7 @@ namespace ICSharpCode.ILSpy
@@ -101,7 +104,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
codeDomBuilder.RunTransformations(transformAbortCondition); |
|
|
|
|
codeDomBuilder.GenerateCode(output); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void DecompileField(FieldDefinition field, ITextOutput output, DecompilationOptions options) |
|
|
|
|
{ |
|
|
|
|
WriteCommentLine(output, TypeToString(field.DeclaringType, includeNamespace: true)); |
|
|
|
|
@ -110,7 +113,7 @@ namespace ICSharpCode.ILSpy
@@ -110,7 +113,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
codeDomBuilder.RunTransformations(transformAbortCondition); |
|
|
|
|
codeDomBuilder.GenerateCode(output); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void DecompileEvent(EventDefinition ev, ITextOutput output, DecompilationOptions options) |
|
|
|
|
{ |
|
|
|
|
WriteCommentLine(output, TypeToString(ev.DeclaringType, includeNamespace: true)); |
|
|
|
|
@ -119,7 +122,7 @@ namespace ICSharpCode.ILSpy
@@ -119,7 +122,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
codeDomBuilder.RunTransformations(transformAbortCondition); |
|
|
|
|
codeDomBuilder.GenerateCode(output); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void DecompileType(TypeDefinition type, ITextOutput output, DecompilationOptions options) |
|
|
|
|
{ |
|
|
|
|
AstBuilder codeDomBuilder = CreateAstBuilder(options, currentType: type); |
|
|
|
|
@ -127,7 +130,7 @@ namespace ICSharpCode.ILSpy
@@ -127,7 +130,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
codeDomBuilder.RunTransformations(transformAbortCondition); |
|
|
|
|
codeDomBuilder.GenerateCode(output); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options) |
|
|
|
|
{ |
|
|
|
|
if (options.FullDecompilation && options.SaveAsProjectDirectory != null) { |
|
|
|
|
@ -146,7 +149,7 @@ namespace ICSharpCode.ILSpy
@@ -146,7 +149,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region WriteProjectFile
|
|
|
|
|
void WriteProjectFile(TextWriter writer, IEnumerable<Tuple<string, string>> files, ModuleDefinition module) |
|
|
|
|
{ |
|
|
|
|
@ -174,20 +177,20 @@ namespace ICSharpCode.ILSpy
@@ -174,20 +177,20 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
w.WriteStartElement("Project", ns); |
|
|
|
|
w.WriteAttributeString("ToolsVersion", "4.0"); |
|
|
|
|
w.WriteAttributeString("DefaultTargets", "Build"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteStartElement("PropertyGroup"); |
|
|
|
|
w.WriteElementString("ProjectGuid", Guid.NewGuid().ToString().ToUpperInvariant()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteStartElement("Configuration"); |
|
|
|
|
w.WriteAttributeString("Condition", " '$(Configuration)' == '' "); |
|
|
|
|
w.WriteValue("Debug"); |
|
|
|
|
w.WriteEndElement(); // </Configuration>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteStartElement("Platform"); |
|
|
|
|
w.WriteAttributeString("Condition", " '$(Platform)' == '' "); |
|
|
|
|
w.WriteValue(platformName); |
|
|
|
|
w.WriteEndElement(); // </Platform>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (module.Kind) { |
|
|
|
|
case ModuleKind.Windows: |
|
|
|
|
w.WriteElementString("OutputType", "WinExe"); |
|
|
|
|
@ -199,7 +202,7 @@ namespace ICSharpCode.ILSpy
@@ -199,7 +202,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
w.WriteElementString("OutputType", "Library"); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteElementString("AssemblyName", module.Assembly.Name.Name); |
|
|
|
|
switch (module.Runtime) { |
|
|
|
|
case TargetRuntime.Net_1_0: |
|
|
|
|
@ -218,14 +221,14 @@ namespace ICSharpCode.ILSpy
@@ -218,14 +221,14 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
w.WriteElementString("WarningLevel", "4"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteEndElement(); // </PropertyGroup>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteStartElement("PropertyGroup"); // platform-specific
|
|
|
|
|
w.WriteAttributeString("Condition", " '$(Platform)' == '" + platformName + "' "); |
|
|
|
|
w.WriteElementString("PlatformTarget", platformName); |
|
|
|
|
w.WriteEndElement(); // </PropertyGroup> (platform-specific)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteStartElement("PropertyGroup"); // Debug
|
|
|
|
|
w.WriteAttributeString("Condition", " '$(Configuration)' == 'Debug' "); |
|
|
|
|
w.WriteElementString("OutputPath", "bin\\Debug\\"); |
|
|
|
|
@ -233,7 +236,7 @@ namespace ICSharpCode.ILSpy
@@ -233,7 +236,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
w.WriteElementString("DebugType", "full"); |
|
|
|
|
w.WriteElementString("Optimize", "false"); |
|
|
|
|
w.WriteEndElement(); // </PropertyGroup> (Debug)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteStartElement("PropertyGroup"); // Release
|
|
|
|
|
w.WriteAttributeString("Condition", " '$(Configuration)' == 'Release' "); |
|
|
|
|
w.WriteElementString("OutputPath", "bin\\Release\\"); |
|
|
|
|
@ -241,8 +244,8 @@ namespace ICSharpCode.ILSpy
@@ -241,8 +244,8 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
w.WriteElementString("DebugType", "pdbonly"); |
|
|
|
|
w.WriteElementString("Optimize", "true"); |
|
|
|
|
w.WriteEndElement(); // </PropertyGroup> (Release)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteStartElement("ItemGroup"); // References
|
|
|
|
|
foreach (AssemblyNameReference r in module.AssemblyReferences) { |
|
|
|
|
if (r.Name != "mscorlib") { |
|
|
|
|
@ -253,7 +256,7 @@ namespace ICSharpCode.ILSpy
@@ -253,7 +256,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
w.WriteEndElement(); // </ItemGroup> (References)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (IGrouping<string, string> gr in (from f in files group f.Item2 by f.Item1 into g orderby g.Key select g)) { |
|
|
|
|
w.WriteStartElement("ItemGroup"); |
|
|
|
|
foreach (string file in gr.OrderBy(f => f, StringComparer.OrdinalIgnoreCase)) { |
|
|
|
|
@ -263,16 +266,16 @@ namespace ICSharpCode.ILSpy
@@ -263,16 +266,16 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
} |
|
|
|
|
w.WriteEndElement(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteStartElement("Import"); |
|
|
|
|
w.WriteAttributeString("Project", "$(MSBuildToolsPath)\\Microsoft.CSharp.targets"); |
|
|
|
|
w.WriteEndElement(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteEndDocument(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region WriteCodeFilesInProject
|
|
|
|
|
bool IncludeTypeWhenDecompilingProject(TypeDefinition type, DecompilationOptions options) |
|
|
|
|
{ |
|
|
|
|
@ -282,11 +285,11 @@ namespace ICSharpCode.ILSpy
@@ -282,11 +285,11 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
return false; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IEnumerable<Tuple<string, string>> WriteCodeFilesInProject(AssemblyDefinition assembly, DecompilationOptions options, HashSet<string> directories) |
|
|
|
|
{ |
|
|
|
|
var files = assembly.MainModule.Types.Where(t => IncludeTypeWhenDecompilingProject(t, options)).GroupBy( |
|
|
|
|
delegate (TypeDefinition type) { |
|
|
|
|
delegate(TypeDefinition type) { |
|
|
|
|
string file = TextView.DecompilerTextView.CleanUpName(type.Name) + this.FileExtension; |
|
|
|
|
if (string.IsNullOrEmpty(type.Namespace)) { |
|
|
|
|
return file; |
|
|
|
|
@ -301,7 +304,7 @@ namespace ICSharpCode.ILSpy
@@ -301,7 +304,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
Parallel.ForEach( |
|
|
|
|
files, |
|
|
|
|
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, |
|
|
|
|
delegate (IGrouping<string, TypeDefinition> file) { |
|
|
|
|
delegate(IGrouping<string, TypeDefinition> file) { |
|
|
|
|
using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, file.Key))) { |
|
|
|
|
AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.MainModule); |
|
|
|
|
foreach (TypeDefinition type in file) { |
|
|
|
|
@ -315,7 +318,7 @@ namespace ICSharpCode.ILSpy
@@ -315,7 +318,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
return files.Select(f => Tuple.Create("Compile", f.Key)); |
|
|
|
|
} |
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region WriteResourceFilesInProject
|
|
|
|
|
IEnumerable<Tuple<string, string>> WriteResourceFilesInProject(LoadedAssembly assembly, DecompilationOptions options, HashSet<string> directories) |
|
|
|
|
{ |
|
|
|
|
@ -329,7 +332,8 @@ namespace ICSharpCode.ILSpy
@@ -329,7 +332,8 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
IEnumerable<DictionaryEntry> rs = null; |
|
|
|
|
try { |
|
|
|
|
rs = new ResourceSet(s).Cast<DictionaryEntry>(); |
|
|
|
|
} catch (ArgumentException) { |
|
|
|
|
} |
|
|
|
|
catch (ArgumentException) { |
|
|
|
|
} |
|
|
|
|
if (rs != null && rs.All(e => e.Value is Stream)) { |
|
|
|
|
foreach (var pair in rs) { |
|
|
|
|
@ -347,7 +351,8 @@ namespace ICSharpCode.ILSpy
@@ -347,7 +351,8 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
string xaml = null; |
|
|
|
|
try { |
|
|
|
|
xaml = decompiler.DecompileBaml(ms, assembly.FileName, new ConnectMethodDecompiler(assembly), new AssemblyResolver(assembly)); |
|
|
|
|
} catch (XamlXmlWriterException) {} // ignore XAML writer exceptions
|
|
|
|
|
} |
|
|
|
|
catch (XamlXmlWriterException) { } // ignore XAML writer exceptions
|
|
|
|
|
if (xaml != null) { |
|
|
|
|
File.WriteAllText(Path.Combine(options.SaveAsProjectDirectory, Path.ChangeExtension(fileName, ".xaml")), xaml); |
|
|
|
|
yield return Tuple.Create("Page", Path.ChangeExtension(fileName, ".xaml")); |
|
|
|
|
@ -368,12 +373,13 @@ namespace ICSharpCode.ILSpy
@@ -368,12 +373,13 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
} |
|
|
|
|
yield return Tuple.Create("EmbeddedResource", fileName); |
|
|
|
|
} |
|
|
|
|
} finally { |
|
|
|
|
} |
|
|
|
|
finally { |
|
|
|
|
if (bamlDecompilerAppDomain != null) |
|
|
|
|
AppDomain.Unload(bamlDecompilerAppDomain); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string GetFileNameForResource(string fullName, HashSet<string> directories) |
|
|
|
|
{ |
|
|
|
|
string[] splitName = fullName.Split('.'); |
|
|
|
|
@ -389,7 +395,7 @@ namespace ICSharpCode.ILSpy
@@ -389,7 +395,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
return fileName; |
|
|
|
|
} |
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AstBuilder CreateAstBuilder(DecompilationOptions options, ModuleDefinition currentModule = null, TypeDefinition currentType = null, bool isSingleMember = false) |
|
|
|
|
{ |
|
|
|
|
if (currentModule == null) |
|
|
|
|
@ -413,7 +419,7 @@ namespace ICSharpCode.ILSpy
@@ -413,7 +419,7 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
if (includeNamespace) |
|
|
|
|
options |= ConvertTypeOptions.IncludeNamespace; |
|
|
|
|
AstType astType = AstBuilder.ConvertType(type, typeAttributes, options); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StringWriter w = new StringWriter(); |
|
|
|
|
if (type.IsByReference) { |
|
|
|
|
ParameterDefinition pd = typeAttributes as ParameterDefinition; |
|
|
|
|
@ -421,11 +427,11 @@ namespace ICSharpCode.ILSpy
@@ -421,11 +427,11 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
w.Write("out "); |
|
|
|
|
else |
|
|
|
|
w.Write("ref "); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (astType is ComposedType && ((ComposedType)astType).PointerRank > 0) |
|
|
|
|
((ComposedType)astType).PointerRank--; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
astType.AcceptVisitor(new OutputVisitor(w, new CSharpFormattingOptions()), null); |
|
|
|
|
return w.ToString(); |
|
|
|
|
} |
|
|
|
|
@ -460,12 +466,20 @@ namespace ICSharpCode.ILSpy
@@ -460,12 +466,20 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
} else |
|
|
|
|
return property.Name; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override bool ShowMember(MemberReference member) |
|
|
|
|
{ |
|
|
|
|
return showAllMembers || !AstBuilder.MemberIsHidden(member, new DecompilationOptions().DecompilerSettings); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override MemberReference GetOriginalCodeLocation(MemberReference member) |
|
|
|
|
{ |
|
|
|
|
if (showAllMembers || !DecompilerSettingsPanel.CurrentDecompilerSettings.AnonymousMethods) |
|
|
|
|
return member; |
|
|
|
|
else |
|
|
|
|
return ICSharpCode.ILSpy.TreeNodes.Analyzer.Helpers.GetOriginalCodeLocation(member); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override string GetTooltip(MemberReference member) |
|
|
|
|
{ |
|
|
|
|
MethodDefinition md = member as MethodDefinition; |
|
|
|
|
@ -486,12 +500,12 @@ namespace ICSharpCode.ILSpy
@@ -486,12 +500,12 @@ namespace ICSharpCode.ILSpy
|
|
|
|
|
b.RunTransformations(); |
|
|
|
|
foreach (var attribute in b.CompilationUnit.Descendants.OfType<AttributeSection>()) |
|
|
|
|
attribute.Remove(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StringWriter w = new StringWriter(); |
|
|
|
|
b.GenerateCode(new PlainTextOutput(w)); |
|
|
|
|
return Regex.Replace(w.ToString(), @"\s+", " ").TrimEnd(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return base.GetTooltip(member); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|