-
Notifications
You must be signed in to change notification settings - Fork 230
[Feedback]: 以下为作者写的简单案例,以方便开发者了解异常处理。 #382
Copy link
Copy link
Open
Labels
feedbackSomething want to say.Something want to say.
Description
♥️ 您觉得咱们这个项目的什么地方作的比较好 (What have you loved)?
public static void Initialize()
{
NatashaManagement
//获取链式构造器
.GetInitializer()
//使用内存程序集中的命名空间
.WithMemoryUsing()
//使用内存中的元数据
.WithMemoryReference()
//注册域构造器
.Preheating<NatashaDomainCreator>();
AssemblyCSharpBuilder assemblyCSharp = new();
try
{
assemblyCSharp
//使用随即域承载编译内容
.UseRandomLoadContext()
//不手动管理元数据
.UseSmartMode()
//使用优化编译
.WithReleaseCompile();
//将编译的日志输出
//assemblyCSharp.LogCompilationEvent += (log) => { Console.WriteLine(log.ToString()); };
//编译失败处理失败信息
assemblyCSharp.CompileFailedEvent += (compilation, diagnostics) => { Console.WriteLine("失败了"); };
assemblyCSharp.Add("public class A{}");
//不注释这一行将出现语法错误
//assemblyCSharp.Add("public class B{ public static void Show(){ Console.WriteLine(\"Hello World!\"); }");
assemblyCSharp.Add("public class B1{ public static void Show(){ Console.WriteLine(\"Hello World!\"); }}");
assemblyCSharp.Add("public class C{ public int Sum(int i,int j){return i+j;} }");
//不注释这一行将出现语义错误
//assemblyCSharp.Add("public class D{ public void Sum(int i,int j){return i+j;} }");
var newAssembly = assemblyCSharp.GetAssembly();
var hwAction = newAssembly.GetDelegateFromShortName<Action>("B1","Show");
hwAction();
var cType = newAssembly.GetType("C");
var cModel = Activator.CreateInstance(cType!);
Console.WriteLine(cType!.GetMethod("Sum")!=null);
}
catch (Exception ex)
{
var natashaEx = assemblyCSharp.GetException();
if (natashaEx!.ErrorKind == NatashaExceptionKind.Syntax)
{
Console.WriteLine("语法错误!");
//输出格式化代码
Console.WriteLine(natashaEx!.Formatter);
Console.WriteLine(natashaEx.Message);
}
else
{
//输出详细的编译日志
//等同于 assemblyCSharp.LogCompilationEvent += (log) => { Console.WriteLine(log.ToString()); };
Console.WriteLine(assemblyCSharp.GetFullLog());
//输出编译信息
Console.WriteLine(natashaEx!.CompileMessage);
}
}
}💢 您觉得那些地方让您感到困惑和迷茫 (What was confusing or gave you pause)?
No response
✨ 那些功能是您比较期待的 (Are there features you'd like to see added)?
No response
🍺 我有酒,请说出你的故事(Show your story).
No response
📞 您的联系方式(Contact Details).
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
feedbackSomething want to say.Something want to say.