Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8681473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:31:09+00:00 2026-06-12T21:31:09+00:00

I am using latest version of Mono.cecil What I am trying to do is

  • 0

I am using latest version of Mono.cecil
What I am trying to do is I am having Custom Attributes in a seperate dll and I want to add the attributes to every method in the sample.exe using mono.cecil
but i am getting the error

“Error ‘System.Void Mono.Cecil.AssemblyDefinition::.ctor()’ is declared in another module and needs to be imported”

Can any one help me in resolving it?

Expecting your help…..

     //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 ()
  {
  }


  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:31:10+00:00Added an answer on June 12, 2026 at 9:31 pm

    The issue is that the lines:

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

    make absolutely no sense.

    Let say you have:

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

    And that you want to add [SampleClass] on each types in SampleExe.exe.

    You need to create a reference from the SampleClass type in sampleDll into 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));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the latest version of Resharper (4.5.x) with VS2008. Every now and then (pretty
I am using wordpress latest version. Currently my blog is set to custom permalink
I'm using the latest version of Gradle (milestone 9), and I'm trying to figure
I'm using the latest version of Firefox and Chrome on Win XP. I want
I am using the latest version of Monodroid, 4.04, and am trying to connect
I developed my app using Mono for Android. I have the latest version 4.0.3.
I am using the latest version of GlassFish. I want to set the Access-Control-Allow-Origin
Without using the latest version of Lucene with faceting enabled, I am trying to
I'm using the latest version of EF and trying to work out how to
i'm using latest svn version (1.6) of zfdebug with zend 1.11.4 and trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.