繁体   English   中英

在 .NET 内核中运行 Mono.Cecil

[英]Run Mono.Cecil in .NET Core

我从使用 .NET Core 2 和 Mono.Cecil 0.10.0-beta7 编译的 SO anwser运行 HelloWorld 控制台应用程序示例:

var myHelloWorldApp = AssemblyDefinition.CreateAssembly(
    new AssemblyNameDefinition("HelloWorld", new Version(1, 0, 0, 0)), "HelloWorld", ModuleKind.Console);

var module = myHelloWorldApp.MainModule;

// create the program type and add it to the module
var programType = new TypeDefinition("HelloWorld", "Program",
    Mono.Cecil.TypeAttributes.Class | Mono.Cecil.TypeAttributes.Public, module.TypeSystem.Object);

module.Types.Add(programType);

// add an empty constructor
var ctor = new MethodDefinition(".ctor", Mono.Cecil.MethodAttributes.Public | Mono.Cecil.MethodAttributes.HideBySig
    | Mono.Cecil.MethodAttributes.SpecialName | Mono.Cecil.MethodAttributes.RTSpecialName, module.TypeSystem.Void);

// create the constructor's method body
var il = ctor.Body.GetILProcessor();

il.Append(il.Create(OpCodes.Ldarg_0));

// call the base constructor
il.Append(il.Create(OpCodes.Call, module.Import(typeof(object).GetConstructor(Array.Empty<Type>()))));

il.Append(il.Create(OpCodes.Nop));
il.Append(il.Create(OpCodes.Ret));

programType.Methods.Add(ctor);

// define the 'Main' method and add it to 'Program'
var mainMethod = new MethodDefinition("Main",
    Mono.Cecil.MethodAttributes.Public | Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void);

programType.Methods.Add(mainMethod);

// add the 'args' parameter
var argsParameter = new ParameterDefinition("args",
    Mono.Cecil.ParameterAttributes.None, module.Import(typeof(string[])));

mainMethod.Parameters.Add(argsParameter);

// create the method body
il = mainMethod.Body.GetILProcessor();

il.Append(il.Create(OpCodes.Nop));
il.Append(il.Create(OpCodes.Ldstr, "Hello World"));

var writeLineMethod = il.Create(OpCodes.Call,
    module.Import(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) })));

// call the method
il.Append(writeLineMethod);

il.Append(il.Create(OpCodes.Nop));
il.Append(il.Create(OpCodes.Ret));

// set the entry point and save the module
myHelloWorldApp.EntryPoint = mainMethod;
myHelloWorldApp.Write("HelloWorld.exe");

上面的代码在使用 .NET Framework 编译时可以正常执行,但是在使用 .NET Core 编译时会出现错误:

未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集“System.Console,版本=4.1.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。 该系统找不到指定的文件。

在 HelloWorld.Program.Main(String[] args)

想问一下,为什么HelloWorld应用找不到汇编文件mscorlib.dll? 我应该怎么做才能修复它?

我通过在HelloWorld.runtimeconfig.json的同一文件夹中创建HelloWorld.exe然后运行dotnet./HelloWorld.exe解决了这个问题。 它会将Hello World打印到控制台。

Json 文件:

{
  "runtimeOptions": {
    "tfm": "netcoreapp2.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "2.0.0"
    }
  }
}

您必须在 .net 经典中执行装配操作,即 .net 4.x。 所以这意味着当你做typeof(Console)是从完整运行时导入Console ,而不是从 NET Core 运行时

如果使用typeof(...) ,则从当前运行时 (.NET Core 2) 导入类型。 If you want to make a .NET Framework application from .NET Core, you need to import mscorlib from .NET Framework on Windows, or mono, and use the types from it. 请参阅我的其他答案: https://stackoverflow.com/a/73434552/4205390

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM