编译后的代码的调用问题
#277
Replies: 1 comment 1 reply
-
@fory77 在其他地方? 指的是哪里? 正常反射是没有问题的,得不到 typeB 先调试一下 asm 里是否有 typeB . |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
你好,作者大大。
我这边有个类代码,已经用natasha编辑进去了
string code = @" using System; namespace WebApplication2.Controllers { public class TestClass { public void Fun1() { Console.WriteLine(""Hello from Natasha!""); } } }";
类如上,预热编译代码如下:
`NatashaManagement.RegistDomainCreator();
NatashaManagement.Preheating(true, true);
Console.WriteLine(NatashaLoadContext.DefaultContext.Domain.Name);
AssemblyCSharpBuilder builder = new();
builder
.UseDefaultDomain()
.ConfigCompilerOption(item => item.WithLowerVersionsAssembly())
.WithDebugCompile(item => item.WriteToAssembly()) //debug下使用
.UseSmartMode() //智能模式(作用:完全引用当前程序集,合并程序集,release模式,语义检测)
;
builder.Add(code);`
直接测试,倒是没问题,测试调用代码如下:
var typeB = asm.GetType("WebApplication2.Controllers.TestClass"); var funcB = typeB.GetMethod("Fun1"); funcB.Invoke(Activator.CreateInstance(typeB), null);
但是,问题是,我在动态编译后,在其他地方,使用反射,想测试一下,却不行,测试代码和上方测试代码一致。。
typeB,也就是类型,是得不到的,一直null。
测试代码:
Type myType = Type.GetType("WebApplication2.Controllers.TestClass"); MethodInfo myMethod = myType.GetMethod("Fun1"); myMethod.Invoke(Activator.CreateInstance(myType), null);
请问该如何处理才对?还望不吝赐教
Beta Was this translation helpful? Give feedback.
All reactions