【问题标题】:"Error 'System.Void Mono.Cecil.AssemblyDefinition::.ctor()' is declared in another module and needs to be imported"“错误 'System.Void Mono.Cecil.AssemblyDefinition::.ctor()' 在另一个模块中声明,需要导入”
【发布时间】:2012-10-12 21:54:07
【问题描述】:

我正在使用最新版本的 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 ()
  {
  }


  }
}

【问题讨论】:

    标签: .net mono.cecil


    【解决方案1】:

    问题在于以下几行:

     var ty = assemblyDefinition.MainModule.Import(as1.GetType ());
     var attCtor = ty.Resolve().Methods.First(x => x.Name == ".ctor" && x.Parameters.Count == 0); 
    

    完全没有意义。

    假设你有:

    var sampleDll = AssemblyDefinition.ReadAssembly ("SampleDll.dll");
    var targetExe = AssemblyDefiniion.ReadAssembly ("SampleExe.exe");
    

    并且您想在SampleExe.exe 中的每种类型上添加[SampleClass]

    您需要创建从sampleDll 中的SampleClass 类型到targetExe 的引用

    var sampleClass = sampleDll.MainModule.GetType ("SampleClass");
    var sampleClassCtor = sampleClass.Methods.First(m => m.IsConstructor);
    
    var ctorReference = targetExe.MainModule.Import (sampleClassCtor);
    
    foreach (var type in targetExe.MainModule.Types) 
        type.CustomAttributes.Add (new CustomAttribute (ctorReference));
    

    【讨论】:

    • 谢谢先生,我一直在寻找这个很长一段时间非常感谢您的指导,还有一件事先生,我正在将 ASPECTDNG 转换为支持最新版本的 Mono.cecil.9 ,你能在转换过程中指导吗?我在转换过程中也面临一些问题......
    • @MuruganAlagesan 请将问题标记为已回答。关于 AspectDNG 转换,恐怕我现在没有时间。但是,如果您有任何关于 mono-cecil 小组的问题,我肯定会提供帮助。
    猜你喜欢
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2016-04-23
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    相关资源
    最近更新 更多