!
Mono.Cecil. - (.exe, .dll)
Nuget
Mono.Cecil , C#, , ECMA CIL.
, :
-
- ,
- ,
- ..
DNlib Mono.Cecil , .NET.
DNlib .NET, Mono.Cecil.
, DNlib , , , .
.NET Core, .NET Standard .NET Framework, Mono.Cecil .NET Framework.
, 1- , 2- .
1)
// StubDnlib - .exe
using System.IO.MemoryStream memoryStream = new(Properties.Resources.StubDnlib);
//
using AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(memoryStream, new ReaderParameters(ReadingMode.Immediate));
2)
//
string path = @"D:\Projects\MonoProject\bin\Debug\myFile.exe";
//
using AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(path, new ReaderParameters(ReadingMode.Immediate));
..:
AssemblyDefinition Mono.Cecil, .NET.
, , , .
, , , , .
, , , .
AssemblyDefinition.ReadAssembly() - .
ReaderParameters- , Mono.Cecil .
ReadingMode.Immediate - , .
Mono.Cecil , , .
AssemblyDefinition CreateAssembly.
AssemblyDefinition.CreateAssembly- , .
: , , .
, .
//
using AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(memoryStream, new ReaderParameters(ReadingMode.Immediate));
//
if (assembly.CustomAttributes.Count > 0)
{
// , TargetFrameworkAttribute
foreach (CustomAttribute attribute in assembly.CustomAttributes.Where(attribute => attribute.AttributeType.Name == "TargetFrameworkAttribute"))
{
// .NetFramework
attribute.ConstructorArguments[0] = new CustomAttributeArgument(assembly.MainModule.TypeSystem.String, ".NETFramework,Version=v4.6.2"); // : .NETFramework,Version=v
assembly.EntryPoint.Module.RuntimeVersion = "v4.0.30319"; // "v4.0.30319"; // "v4.8.3928.0"
assembly.EntryPoint.Module.Runtime = TargetRuntime.Net_4_0; // Net_4_0
}
}
:
//
foreach (CustomAttribute list in assembly.CustomAttributes)
{
/* : $"{list.AttributeType.Name}\r\n" */
//
if (list.AttributeType.Name.Contains("TargetFrameworkAttribute"))
{
// String
// : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP - .NetFramework, , .NetFramework'a
list.ConstructorArguments[0] = new CustomAttributeArgument(assembly.MainModule.TypeSystem.String, ".NETFramework,Version=v4.6.2");
assembly.EntryPoint.Module.RuntimeVersion = "v4.0.30319"; // "v4.0.30319"; // "v4.8.3928.0"
assembly.EntryPoint.Module.Runtime = TargetRuntime.Net_4_0; // Net_4_0
}
// .. .
}
Linq
// "TargetFrameworkAttribute"
CustomAttribute attr = assembly.CustomAttributes.Where(c => c.AttributeType.Name == "TargetFrameworkAttribute").FirstOrDefault();
//
attr.ConstructorArguments[0] = new CustomAttributeArgument(attr.ConstructorArguments[0].Type, ".NETFramework,Version=v4.6.2");
//
assembly.EntryPoint.Module.RuntimeVersion = "v4.0.30319"; // "v4.8.3928.0", "v2.0.50727.4927" ..
assembly.EntryPoint.Module.Runtime = TargetRuntime.Net_4_0; // Net_4_0
..:
* ConstructorArguments- , .
, .
* CustomAttributeArgument . : .
, , . TypeReference.
* EntryPoint-
* RuntimeVersion- (CLR), . ( Runtime Version vX.X.XXXXX, CLR, .)
* Runtime- , .
* CustomAttributes- , .
, , , , .
, , , ..
* MainModule.TypeSystem.String - System.String Mono.Cecil. .
, .
: AssemblyInfo.cs : ILSpy, DnSpy .
.NET:
CompilationRelaxationsAttribute - int
RuntimeCompatibilityAttribute - bool
DebuggableAttribute - enum
AssemblyTitleAttribute - string
AssemblyDescriptionAttribute - string
AssemblyConfigurationAttribute - string
AssemblyCompanyAttribute - string
AssemblyProductAttribute - string
AssemblyCopyrightAttribute - string
AssemblyTrademarkAttribute - string
ComVisibleAttribute - bool
GuidAttribute - Guid
AssemblyVersion - string
AssemblyFileVersionAttribute - string
TargetFrameworkAttribute - string
Spoiler:
, -
assembly.Write("application.exe"); // Write - .