Код: Выделить всё
public static void BuildExe()
{
try
{
PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilder(_assemblyName);
TypeBuilder tb = ab.DefineDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public | TypeAttributes.Class);
MethodBuilder mb1 = tb.DefineMethod("SumMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(int), [typeof(int), typeof(int)]);
ILGenerator il = mb1.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Ret);
MethodBuilder entryPoint = tb.DefineMethod("Main", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Static, typeof(int), null);
ILGenerator il2 = entryPoint.GetILGenerator();
il2.Emit(OpCodes.Ldc_I4_S, 10);
il2.Emit(OpCodes.Ldc_I4_2);
il2.Emit(OpCodes.Call, mb1);
il2.Emit(OpCodes.Ret);
tb.CreateType();
MetadataBuilder metadataBuilder = ab.GenerateMetadata(out BlobBuilder ilStream, out BlobBuilder fieldData);
PEHeaderBuilder peHeaderBuilder = new PEHeaderBuilder(
imageCharacteristics: Characteristics.ExecutableImage,
subsystem: Subsystem.WindowsCui);
ManagedPEBuilder peBuilder = new ManagedPEBuilder(
header: peHeaderBuilder,
metadataRootBuilder: new MetadataRootBuilder(metadataBuilder),
ilStream: ilStream,
mappedFieldData: fieldData,
entryPoint: MetadataTokens.MethodDefinitionHandle(entryPoint.MetadataToken));
BlobBuilder peBlob = new BlobBuilder();
peBuilder.Serialize(peBlob);
// in case saving to a file:
//using var stream = new MemoryStream();
using var stream = new FileStream(@"c:\temp\mine.exe", FileMode.Create);
peBlob.WriteContentTo(stream);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Код: Выделить всё
Failed to run as a self-contained app.
- The application was run as a self-contained app because 'C:\temp\myassembly2.runtimeconfig.json' was not found.
- If this should be a framework-dependent app, add the 'C:\temp\myassembly2.runtimeconfig.json' file and specify the appropriate framework.
Код: Выделить всё
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\temp\'.
Failed to run as a self-contained app.
- The application was run as a self-contained app because 'C:\temp\myassembly2.runtimeconfig.json' was not found.
- If this should be a framework-dependent app, add the 'C:\temp\myassembly2.runtimeconfig.json' file and specify the appropriate framework.
Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/790 ... s-when-i-t
Мобильная версия