Skip to content

Commit

Permalink
fix: null reference exception when code includes comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jevonsflash committed Aug 24, 2024
1 parent 15fe3ea commit 65a1110
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
14 changes: 6 additions & 8 deletions RoslynSyntaxTool/Manager/CompilationUtilitys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public static MemoryStream CompileClientProxy(IEnumerable<SyntaxTree> trees)
MetadataReference.CreateFromFile(typeof(SyntaxFactory).GetTypeInfo().Assembly.Location)
};
var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
var allReferences = assemblies.Where(c => !c.IsDynamic).Select(x => {
var f = MetadataReference.CreateFromFile(x.Location);
return f;
var allReferences = assemblies.Where(c => !c.IsDynamic).Select(x =>
{
var f = MetadataReference.CreateFromFile(x.Location);
return f;
}).Concat(references);
return Compile(AssemblyInfo.Create("RoslynSyntaxTool.Proxy.ConvertToCSharpProxy"), trees, allReferences);
}
Expand All @@ -44,11 +45,8 @@ public static MemoryStream Compile(string assemblyName, AssemblyInfo assemblyInf
var result = compilation.Emit(stream);
if (!result.Success)
{
foreach (var message in result.Diagnostics.Select(i => i.ToString()))
{
System.Console.WriteLine(message);
}
return null;
var msgs = result.Diagnostics.Select(i => i.ToString());
throw new Exception("语法树动态编译出错\r\n===Start===\r\n"+ string.Join("\r\n", msgs)+"\r\n===End===\r\n请将源码和报错信息反馈至开发者,谢谢\r\n\r\n");
}
stream.Seek(0, SeekOrigin.Begin);
return stream;
Expand Down
66 changes: 44 additions & 22 deletions RoslynSyntaxTool/ViewModel/ConvertToCSharpPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ControlzEx.Standard;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Highlighting;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -88,6 +89,20 @@ private void ContinueAction()
.WithUsings(
SyntaxFactory.List<UsingDirectiveSyntax>(
new UsingDirectiveSyntax[]{
SyntaxFactory.UsingDirective(
SyntaxFactory.IdentifierName("System"))
.WithUsingKeyword(
SyntaxFactory.Token(
SyntaxFactory.TriviaList(),
SyntaxKind.UsingKeyword,
SyntaxFactory.TriviaList(
SyntaxFactory.Space)))
.WithSemicolonToken(
SyntaxFactory.Token(
SyntaxFactory.TriviaList(),
SyntaxKind.SemicolonToken,
SyntaxFactory.TriviaList(
SyntaxFactory.CarriageReturnLineFeed))),
SyntaxFactory.UsingDirective(
SyntaxFactory.QualifiedName(
SyntaxFactory.IdentifierName("Microsoft"),
Expand Down Expand Up @@ -264,39 +279,46 @@ private void ContinueAction()
var stream = CompilationUtilitys.CompileClientProxy(new List<SyntaxTree>() { treeFrame });
using (stream)
if (stream!=null)
{
var assembly = Assembly.Load(stream.ToArray());
_generatedServiceProxyAssembly = assembly;
using (stream)
{
var assembly = Assembly.Load(stream.ToArray());
_generatedServiceProxyAssembly = assembly;
}
var serviceProxyTypes = _generatedServiceProxyAssembly.GetExportedTypes();
}
var serviceProxyTypes = _generatedServiceProxyAssembly.GetExportedTypes();
object serviceProxyObject = null;
Type serviceProxyObjectType = null;
object serviceProxyObject = null;
Type serviceProxyObjectType = null;
foreach (var serviceProxyType in serviceProxyTypes)
{
var typeInfo = serviceProxyType.GetTypeInfo();
if (typeInfo.FullName=="RoslynSyntaxTool.Process.ProxyTreeGen")
foreach (var serviceProxyType in serviceProxyTypes)
{
serviceProxyObjectType=serviceProxyType;
var instance = serviceProxyType.GetTypeInfo().GetConstructors().First().Invoke(null);
serviceProxyObject=instance;
}
var typeInfo = serviceProxyType.GetTypeInfo();
if (typeInfo.FullName=="RoslynSyntaxTool.Process.ProxyTreeGen")
{
serviceProxyObjectType=serviceProxyType;
var instance = serviceProxyType.GetTypeInfo().GetConstructors().First().Invoke(null);
serviceProxyObject=instance;
}
}
var result = "没有结果,请检查输入代码";
var processor = serviceProxyObjectType.GetMethod("Process");
if (processor!=null)
{
result = processor.Invoke(serviceProxyObject, null).ToString();
}
responseText =result;
}
var result = "没有结果,请检查输入代码";
var processor = serviceProxyObjectType.GetMethod("Process");
if (processor!=null)
else
{
result = processor.Invoke(serviceProxyObject, null).ToString();
}
responseText ="语法树动态编译出错,检查 EmitResult.Diagnostics 属性。请将源码和报错信息反馈至开发者,谢谢";
responseText =result;
}
}
catch (Exception ex)
{
Expand Down

0 comments on commit 65a1110

Please sign in to comment.