Mono.cecil の最新バージョンを使用しています 私がやろうとしているのは、別の dll にカスタム属性があり、mono.cecil を使用して sample.exe のすべてのメソッドに属性を追加したいのですが、エラーが発生しています
「エラー 'System.Void Mono.Cecil.AssemblyDefinition::.ctor()' は別のモジュールで宣言されており、インポートする必要があります」
誰かがそれを解決するのを手伝ってくれますか?
あなたの助けを期待しています.....
//Main File Sample.exe
class SampleItems
{
public static void Main()
{
}
public void Sample()
{
Console.WriteLine("sample");
}
public void Sample1()
{
Console .WriteLine ("sample1");
}
}
================================
Seperate application applying the custom attribute
static void Main(string[] args)
{
//opens the assembly
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(@"E:\MONOCECIL\POC SAMPLES\Sample.exe.exe");
//AssemblyDefinition as1= AssemblyDefinition .ReadAssembly (@"E:\MONOCECIL\POC SAMPLES\MonoStart\MonoStart\bin\Debug\SampleDll.dll");
var resolver = new DefaultAssemblyResolver();
var systemName = AssemblyNameReference.Parse("SampleDll");
AssemblyDefinition as1 = resolver.Resolve(systemName);
var ty = assemblyDefinition.MainModule.Import(as1.GetType ());
var attCtor = ty.Resolve().Methods.First(x => x.Name == ".ctor"
&& x.Parameters.Count == 0);
foreach (var modDef in assemblyDefinition.Modules)
{
foreach (var typeDef in modDef.Types)
{
Console.WriteLine(" +--Type {0}", typeDef.Name);
//All methods in this type
foreach (var methodDef in typeDef.Methods)
{
var custatt = new CustomAttribute(attCtor);
methodDef.CustomAttributes.Add(custatt);
Console.WriteLine(" --Method {0}", methodDef.Name);
}
}
}
assemblyDefinition.Write(@"E:\POSTSHARP SAMPLES\ASPECTDNG\Debug\Sampleupdate.exe");
Console .ReadLine ();
}
===============================
Sample dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SampleDll
{
[System.AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple=false )]
public sealed class SampleClass :Attribute
{
public SampleClass ()
{
}
}
}